You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Imagine a non const enum (which can be accessed using properties and it's index signature).
enumexample{ONE=1,TWO=2,THREE=3}consta=example.ONE;console.log(a);// Prints 1constaName=example[a];// Type of aName is string and should be "ONE" | "TWO" | "THREE". Or even "ONE" since a is constconsole.log(aName);// Prints "ONE";
The implicit type of aName is string and should be "ONE" | "TWO" | "THREE" since those are the only possible values. Or even "ONE" since a is a constant.
Use Cases
More control over the values of a variable.
Examples
See above
Checklist
My suggestion meets these guidelines:
This wouldn't be a breaking change in existing TypeScript/JavaScript code
This wouldn't change the runtime behavior of existing JavaScript code
This could be implemented without emitting different JS based on the types of the expressions
This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
A similar and potentially more serious example, I think? Let's say I use an enum to hold various locales:
enumLocale{"en": "en","zh-CN": "zh-CN"}letaVar=Locale["fake-lang"];// Expected an error, but `aVar` is type `any`?letaMap=newMap<Locale,number>([[Locale["en"],1],[Locale["zh-CN"],2],[Locale["fake-lang"],3]// Expected an error, but `Locale["fake-lang'] is type `any`?]);
This is definitely not what I would have expected. I would have expected the compiler to throw an error in this case.
Search Terms
enum index signature
Suggestion
Imagine a non const enum (which can be accessed using properties and it's index signature).
The implicit type of aName is string and should be "ONE" | "TWO" | "THREE" since those are the only possible values. Or even "ONE" since a is a constant.
Use Cases
More control over the values of a variable.
Examples
See above
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: