-
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
Allow string enum in element access #18029
Conversation
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.
src/compiler/checker.ts
Outdated
@@ -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)) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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'.
Fixes #16760
Previously literal union types were disallowed to improve errors by printing
boolean
instead oftrue | false
. But string enums are literal union types that should be allowed, so now only booleans are disallowed.