Reputation: 11599
The following code is from ngrx example.
What this declaration will do? Is this equivalent to dictionary or hashtable in C#?
let typeCache: { [label: string]: boolean } = {};
The original code:
let typeCache: { [label: string]: boolean } = {};
export function type<T>(label: T | ''): T {
if (typeCache[<string>label]) {
throw new Error(`Action type "${label}" is not unique"`);
}
typeCache[<string>label] = true;
return <T>label;
}
Upvotes: 3
Views: 5878
Reputation: 164237
I'm not sure about the C# equivalence, but what it means in typescript is a regular javascript object with boolean properties, and it's referred as Indexable Types.
The keys can only be strings or numbers, this won't compile:
let typeCache: { [label: Date]: boolean } = {}; // error: An index signature parameter type must be 'string' or 'number'
Examples of how a value would look like:
type Indexable = { [label: string]: boolean };
let typeCache1: Indexable = { a: true, b: false };
let typeCache2: Indexable = { a: true, b: "string" }; // error: Type '{ a: true; b: "string"; }' is not assignable to type 'Indexable'
Upvotes: 6
Reputation:
this is map in javascript, a simple key/value map. for details, refer here.
Upvotes: 0