Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct typing when using the index signature of an Enum #31285

Open
5 tasks done
JAciv opened this issue May 7, 2019 · 1 comment
Open
5 tasks done

Correct typing when using the index signature of an Enum #31285

JAciv opened this issue May 7, 2019 · 1 comment
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript

Comments

@JAciv
Copy link

JAciv commented May 7, 2019

Search Terms

enum index signature

Suggestion

Imagine a non const enum (which can be accessed using properties and it's index signature).

enum example { ONE = 1, TWO = 2, THREE = 3 }

const a = example.ONE;
console.log(a); // Prints 1

const aName = example[a]; // Type of aName is string and should be "ONE" | "TWO" | "THREE". Or even "ONE" since a is const
console.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.)
  • This feature would agree with the rest of TypeScript's Design Goals.
@weswigham weswigham added In Discussion Not yet reached consensus Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. Suggestion An idea for TypeScript labels May 8, 2019
@RyanCavanaugh RyanCavanaugh removed the Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. label Jul 16, 2019
@craxal
Copy link

craxal commented Sep 30, 2019

A similar and potentially more serious example, I think? Let's say I use an enum to hold various locales:

enum Locale {
  "en": "en",
  "zh-CN": "zh-CN"
}

let aVar = Locale["fake-lang"]; // Expected an error, but `aVar` is type `any`?
let aMap = new Map<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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants