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

Allow string enum in element access #18029

Merged
merged 4 commits into from
Aug 28, 2017

Conversation

sandersn
Copy link
Member

Fixes #16760

Previously literal union types were disallowed to improve errors by printing boolean instead of true | false. But string enums are literal union types that should be allowed, so now only booleans are disallowed.

Previously literal union types were disallowed to improve errors by
printing `boolean` instead of `true | false`. But string enums are
literal union types that should be allowed, so now only booleans are
disallowed.
@@ -7688,7 +7688,7 @@ namespace ts {
}
// In the following we resolve T[K] to the type of the property in T selected by K.
const apparentObjectType = getApparentType(objectType);
if (indexType.flags & TypeFlags.Union && !(indexType.flags & TypeFlags.Primitive)) {
if (indexType.flags & TypeFlags.Union && !(indexType.flags & TypeFlags.Boolean)) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a comment explaining that we are avoiding TypeFlags.Boolean here for stylistic reasons to ensure that we show boolean and not true in error messages? Might also want to link to keyofAndIndexedAccessErrors.errors.txt, which is the only test that fails (i.e., has slightly different error messages) if this check isn't here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Booleans are not treated like other unions in order to skip straight to
error reporting so that the error is reported with 'boolean' instead of
'true'.
@sandersn sandersn merged commit 278dcc6 into master Aug 28, 2017
@sandersn sandersn deleted the allow-string-enum-in-element-access branch August 28, 2017 19:56
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants