-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Enum indexer return type should be string|undefined
for --strictNullChecks
#11140
Comments
I've fixed this in a local TSC build, and would love to propose a PR. |
IIRC it's the same as why array indexing also returns |
Hard to square this with the fact that people continuously want |
@RyanCavanaugh Thank you for your reply. Note that the enum SomeEnum { A = 1 }
SomeEnum["A"].toFixed(2); // => '1.00'
SomeEnum["A"]._invalid_; // Error: "Property '_invalid_' does not exist on type 'SomeEnum'"
SomeEnum["A".trim()]._invalid_; // No error because this indexer-result type is `any`; will evaluate to `undefined` at run-time
SomeEnum["X"]._invalid_; // No error because this indexer-result type is `any`; will throw "Cannot read property '_invalid_' of undefined" at run-time Further note that for the last two accesses ( @Arnavion Yes, the same applies to the array-indexer, which I think should also be discussed. I wanted to start with the enum "lookup" indexer because I think in this case the chance of a missing undefined-check is higher than for missing undefined- or bounds-checks for array-indexer-accesses. |
I fell over this issue today. We're using objects as maps and define an interface for it, something like this:
Now when accessing I understand it might be annoying for people to write This is the same case with arrays: I wish that |
You could write |
Duplicate #9235 |
@RyanCavanaugh I don't think that this issue is a duplicate. The referenced issue is more general and concerns all indexer (return) types. This issue just concerns the number-indexer of enums and can also be fixed just for that (did this already in a local TS build). Further note that the main argument in #9235 ("break the very common case of arrays") does not apply here. |
TypeScript Version: 2.0.3
Code
Expected behavior:
When using --strictNullChecks, TSC should emit a compile-time error in
lookUp()
because there is noundefined
-check. => Indexer return type should bestring|undefined
instead of juststring
.The text was updated successfully, but these errors were encountered: