Index Types


TypeScript supports indexable types which have an index signature to allow for objects with string or number keys. This can be useful when you don't know in advance what the keys of the object might be.

interface StringDictionary {
    [index: string]: string;
}

Exercise

Create an interface for a dictionary which has string keys and values can be either strings or numbers. Then, create a function to return the value for a given key from this dictionary.


Copyright © learn-ts.org. Read our Terms of Use and Privacy Policy