Repository navigation
Add distributive conditional type relationship rules - #64984
Wesley Wigham (weswigham) wants to merge 3 commits into
Conversation
|
TypeScript Bot (@typescript-bot) test this |
There was a problem hiding this comment.
🟡 Changes recommended
Critical flaws in intersection inference and dependent infer constraints permit unsound generic assignments even when the corresponding concrete conditional types resolve to never.
2 open issues
What changed in this PR
Extends the native TypeScript checker’s assignability rules for distributive conditional types, revisiting #27932 while accounting for never, correlated union members, and inference constraints.
Changes:
- Adds branch-wise conditional checks and a dedicated inference-pattern relation.
- Tracks mapper state and replays cached effects to preserve type-parameter occurrence counts.
- Adds regression coverage and updates baselines, with comment-only clarifications in existing fixtures.
| File | Description |
|---|---|
tsc/testdata/tests/cases/compiler/excessPropertyCheckingIntersectionWithConditional.ts |
Clarifies the expected error in a comment. |
tsc/testdata/tests/cases/compiler/distributiveConditionalTypeRelations.ts |
Adds distribution, never, and inference regression cases. |
tsc/testdata/tests/cases/compiler/distributiveConditionalTypeInferenceNonStrict.ts |
Covers inference under non-strict checking. |
tsc/testdata/tests/cases/compiler/conditionalTypeAssignabilityWhenDeferred.ts |
Clarifies expected behavior in comments. |
tsc/testdata/baselines/reference/compiler/excessPropertyCheckingIntersectionWithConditional.types |
Refreshes embedded comments. |
tsc/testdata/baselines/reference/compiler/excessPropertyCheckingIntersectionWithConditional.symbols |
Refreshes embedded comments. |
tsc/testdata/baselines/reference/compiler/excessPropertyCheckingIntersectionWithConditional.js |
Reflects the comment change in emitted output. |
tsc/testdata/baselines/reference/compiler/excessPropertyCheckingIntersectionWithConditional.errors.txt |
Records the rejected conditional assignment. |
tsc/testdata/baselines/reference/compiler/distributiveConditionalTypeRelations.types |
Records types for the new regression cases. |
tsc/testdata/baselines/reference/compiler/distributiveConditionalTypeRelations.errors.txt |
Records expected assignment errors. |
tsc/testdata/baselines/reference/compiler/distributiveConditionalTypeInferenceNonStrict.types |
Records non-strict inference types. |
tsc/testdata/baselines/reference/compiler/distributiveConditionalTypeInferenceNonStrict.symbols |
Records non-strict test symbols. |
tsc/testdata/baselines/reference/compiler/distributiveConditionalTypeInferenceNonStrict.errors.txt |
Records non-strict inference errors. |
tsc/testdata/baselines/reference/compiler/conditionalTypeAssignabilityWhenDeferred.types |
Refreshes explanatory comments. |
tsc/testdata/baselines/reference/compiler/conditionalTypeAssignabilityWhenDeferred.symbols |
Updates comments and declaration positions. |
tsc/testdata/baselines/reference/compiler/conditionalTypeAssignabilityWhenDeferred.js |
Reflects fixture comment changes. |
tsc/testdata/baselines/reference/compiler/conditionalTypeAssignabilityWhenDeferred.errors.txt |
Records additional never-related rejections. |
tsc/internal/checker/types.go |
Adds inference markers and conditional caches. |
tsc/internal/checker/types_test.go |
Tests inference relations and mapper cache effects. |
tsc/internal/checker/relater.go |
Implements conditional and inference-pattern relationship rules. |
tsc/internal/checker/mapper.go |
Adds mapper state capture and replay hooks. |
tsc/internal/checker/checker.go |
Integrates rigid inference mapping and state-aware caching. |
Review effort: Balanced
|
Hey Wesley Wigham (@weswigham), the results of running the DT tests are ready. Everything looks the same! |
|
Wesley Wigham (@weswigham) Here are the results of running the user tests with tsc comparing Something interesting changed - please have a look. Details
|
|
Wesley Wigham (@weswigham), the perf run you requested failed. You can check the log here. |
|
Wesley Wigham (@weswigham) Here are the results of running the top 400 repos with tsc comparing Something interesting changed - please have a look. Details
|
|
TypeScript Bot (@typescript-bot) perf test this faster |
|
Jake Bailey (@jakebailey) Here they are:
Provisional hosted-runner results: these VMs are not the dedicated benchmark machines. Results may be noisier and are not comparable to the historical series. For comparisons, both revisions of each scenario ran on the same VM. tscComparison Report - baseline..pr
System info unknown
Hosts
Scenarios
Developer Information: |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
TypeScript Bot (@typescript-bot) test this |
There was a problem hiding this comment.
🔵 Needs a closer look
Changes to core assignability semantics and stateful mapper-effect replay warrant compiler-maintainer review plus compiler-wide regression and performance validation unavailable in this environment.
No open issues 🎉
2 resolved since last review
Review effort: Balanced
|
Hey Wesley Wigham (@weswigham), the results of running the DT tests are ready. Everything looks the same! |
|
Wesley Wigham (@weswigham) Here are the results of running the user tests with tsc comparing Something interesting changed - please have a look. Details
|
|
Wesley Wigham (@weswigham) Here they are:
tscComparison Report - baseline..pr
System info unknown
Hosts
Scenarios
Developer Information: |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Wesley Wigham (@weswigham) Here are the results of running the top 400 repos with tsc comparing Everything looks good! |

This is basically a redo of #27932, though updated and better integrated with the existing relationship rules.
This change allows a source type
Sto be assigned to a target typeTwhen it is a distributive conditional type and:Sis assignable to all possible distributed true and/or false branch typesSis possiblyneverfor the same inputs that makeT's check typeneverorScannot ever becomeneverthrough instantiation (eg, becauseShas top-level structure)S"distributes" in a compatible way with the conditional distribution (eg,{x: string | number}is related to{x: string} | {x: number}, but{x: string | number, y: string | number}is not related to{x: string, y: string} | {x: number, y: number})infertype parameters don't have inference results/constraints that would invalidate the aboveTo support that, we introduce a function type mapper that can track how many times a type parameter appears in a type (so, eg, you can know that
[T, T]can't relate toT extends T ? [T, T] : neverbecause the distribution target appears in multiple places, so unions without and within can't be equivalent), and a new, semi-strict relationship rule for relatinginferpositions strictly, but otherwise relate as loose as assignability, generally.