0

In typescript documentation , like a variable type is defined as for e.g.:-

let var:number

in which var is of type number, but in the code mentioned below:-

namespace Validation {
    export interface StringValidator {
        isAcceptable(s: string): boolean;
    }

    const lettersRegexp = /^[A-Za-z]+$/;
    const numberRegexp = /^[0-9]+$/;

    export class LettersOnlyValidator implements StringValidator {
        isAcceptable(s: string) {
            return lettersRegexp.test(s);
        }
    }

    export class ZipCodeValidator implements StringValidator {
        isAcceptable(s: string) {
            return s.length === 5 && numberRegexp.test(s);
        }
    }
}

// Some samples to try
let strings = ["Hello", "98052", "101"];

// Validators to use
let validators: { [s: string]: Validation.StringValidator; } = {};
validators["ZIP code"] = new Validation.ZipCodeValidator();
validators["Letters only"] = new Validation.LettersOnlyValidator();

// Show whether each string passed each validator
for (let s of strings) {
    for (let name in validators) {
        console.log(`"${ s }" - ${ validators[name].isAcceptable(s) ? "matches" : "does not match" } ${ name }`);
    }
}

In the above code , what is the meaning of the statement :-

let validators: { [s: string]: Validation.StringValidator; } = {};

Thanks for the help.

1
  • 4
    Just a small remark, in your example 'let var: number' is not correct js/ts because var is a reserved word. Commented Jul 10, 2018 at 12:05

2 Answers 2

4

This is a string index signature. This means that you can set any property on a variable of this type as long as the property name is a string (it has to be anyway) and the property value is of type Validation.StringValidator.

You can see this on the subsequent lines, validators["ZIP code"] = new Validation.ZipCodeValidator(); -- this sets a string property "ZIP code" to a value of ZipCodeValidator which must extend from StringValidator.

The = {} part just assigns an empty object to validator at first. This is done so that you can set properties on it. If you didn't set it to an empty object, you would get an error when you tried to write to validators["ZIP code"], etc.

Sign up to request clarification or add additional context in comments.

5 Comments

"...as the property name is a string (it has to be anyway)..." Not for a few years now, ES2015 added Symbol-named properties.
@T.J.Crowder good point; seems like TypeScript doesn't support this for index signatures yet
If I am not wrong the property name will internally be casted to a string, right?
@A.Llorente - Property names can be strings or Symbols. If you use something that isn't a string or a Symbol as a property name (for instance, a[2]), it's coerced (converted) to string and then that string is used. Symbols are not converted to strings.
@ExplosionPills - Yeah, just tried it in the playground, and it complains it wants string or number (despite the fact that property names are never numbers, even though we routinely write them as numbers, for instance when using arrays).
2

It is saying that the validators is an object (of type Object) whose keys are of type String and the value of type StringValidator

So basically it is just setting the type, same as let a: number; is saying that a is a number but with a more complex object type.

2 Comments

It may be better to say that validators is an object whose keys are of type string and values of type Validation.StringValidator. I tripped over your phrasing the first time because I thought you were saying it has a single key s whose type is string.
@Aankhen I have rephrased following your suggestion, thanks! T.J. Crowder as per Explosion Pillls reply (which is better than mine) it is in the handbook: typescriptlang.org/docs/handbook/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.