Skip to content

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

Merged
merged 5 commits into from
May 22, 2018

Conversation

weswigham
Copy link
Member

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)

@weswigham weswigham requested review from ahejlsberg and mhegazy May 14, 2018 17:10
@ahejlsberg
Copy link
Member

I'm not sure I like this. It's really odd that T extends string and T extends string | number | symbol gives you literal types, but T extends string | number does not!

@weswigham weswigham force-pushed the keyof-contextual-type-fix branch from 7332390 to 23510e8 Compare May 17, 2018 22:43
@weswigham
Copy link
Member Author

@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, user tests, or rwc tests. I think it's a relatively safe change.

Copy link
Member

@ahejlsberg ahejlsberg left a 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) ||
Copy link
Member

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.

@weswigham weswigham merged commit 0102f80 into microsoft:master May 22, 2018
@weswigham weswigham deleted the keyof-contextual-type-fix branch May 22, 2018 23:58
weswigham added a commit to weswigham/TypeScript that referenced this pull request May 23, 2018
…#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
mhegazy added a commit that referenced this pull request May 23, 2018
Check for keyof constraint type instead of syntactic check (#24098)
@microsoft microsoft locked and limited conversation to collaborators Jul 31, 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.

2 participants