-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Check for keyof constraint type instead of syntactic check #24098
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
Check for keyof constraint type instead of syntactic check #24098
Conversation
I'm not sure I like this. It's really odd that |
7332390
to
23510e8
Compare
@ahejlsberg So I've generalized it to just being a type variable constrained to any of string or number or symbol (rather than exactly one of them)... and there's no diff to any tests, |
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.
Approved with the noted change.
return isTypeParameterWithKeyofConstraint(contextualType) && maybeTypeOfKind(candidateType, TypeFlags.StringLiteral | TypeFlags.NumberLiteral | TypeFlags.UniqueESSymbol) || | ||
constraint.flags & TypeFlags.String && maybeTypeOfKind(candidateType, TypeFlags.StringLiteral) || | ||
constraint.flags & TypeFlags.Number && maybeTypeOfKind(candidateType, TypeFlags.NumberLiteral) || | ||
constraint.flags & TypeFlags.Boolean && maybeTypeOfKind(candidateType, TypeFlags.BooleanLiteral) || |
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.
The line that checks for TypeFlags.Boolean
isn't necessary. The logic already covers unions of literal types, and TypeFlags.Boolean
is just a convenience flag we set on the built-in true | false
union.
…#24098) * Check for keyof constraint type instead of syntactic check * Readopt older candidateType check, even though it shouldnt really matter * OK. Just use maybetypeOfKind * Remove redundant boolean check
Check for keyof constraint type instead of syntactic check (#24098)
Fixes #24080
Downside:
T extends string | symbol | number
will now produce literals. (technically a break)Upside:
T extends string | symbol | number
will now produce literals. (probably desirably, given any one of those on their own would produce literals)