-
Notifications
You must be signed in to change notification settings - Fork 13k
Support contextual type discrimination based on template literal expressions #62203
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
base: main
Are you sure you want to change the base?
Support contextual type discrimination based on template literal expressions #62203
Conversation
…`isPatternLiteralPlaceholderType`
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.
Pull Request Overview
This PR adds support for contextual type discrimination based on template literal types. It allows template literal expressions to be used to discriminate contextual types, fixing cases where template literal expressions couldn't properly narrow union types during object literal type checking.
Key changes:
- Introduces a new
contextFreeType
intrinsic type to handle context-free template literal type inference - Modifies template literal type checking to prevent widening to
string
when template literals can serve as discriminants - Updates
getContextFreeType
to use the new context-free type instead ofanyType
Reviewed Changes
Copilot reviewed 14 out of 14 changed files in this pull request and generated no comments.
Show a summary per file
File | Description |
---|---|
src/compiler/checker.ts |
Adds contextFreeType intrinsic and updates template literal type checking logic to preserve discriminant information |
tests/cases/conformance/types/union/discriminatedUnionTypesOverlappingDiscriminants1.ts |
New comprehensive test cases for template literal discriminants in various scenarios |
tests/cases/compiler/templateLiteralConstantEvaluation.ts |
Adds test case for empty string template literal evaluation |
tests/cases/compiler/evolvingArrayTemplateLiterals1.ts |
New test for template literal behavior in evolving arrays |
tests/cases/compiler/discriminantUsingEvaluatableTemplateExpression.ts |
Adds test for evaluatable template expression discrimination |
Various baseline files | Expected type checking results and error outputs for the new test cases |
Comments suppressed due to low confidence (2)
@typescript-bot test it |
Hey @jakebailey, the results of running the DT tests are ready. Everything looks the same! |
@jakebailey Here are the results of running the user tests with tsc comparing Everything looks good! |
@jakebailey Here they are:
tscComparison Report - baseline..pr
System info unknown
Hosts
Scenarios
Developer Information: |
@jakebailey Here are the results of running the top 400 repos with tsc comparing Everything looks good! |
Template literal types are allowed as discriminant properties since #46137
but template literal expressions can't be used to discriminate contextual types which manifests in issues like:
This PR closes the gap by preventing potential template literal types discriminants from widening to
string
in the context of context-free types. I don't think this changes all that much the meaning of a context-free type is. Let's consider:The context-free type of that
"_FOO"
expression is a fresh"_FOO"
string literal type. In the same fashion, a context-free type of a template literal expression can be that unwidened template literal type.fixes #57231