Repository navigation
Distinguish between non-distributed and distributed type parameters in mapped types too - #65666
Mateusz Burzyński (Andarist) wants to merge 13 commits into
Conversation
…e them through the shared helper
… in reverse mapped types
|
This PR doesn't have any linked issues. Please open an issue that references this PR. From there we can discuss and prioritise. |
There was a problem hiding this comment.
🟡 Changes recommended
Parenthesized mapped-type constraints still permit unsafe instantiations, while lazy distributed-type handling can reject valid assignments and make inference query-order dependent.
2 open issues
What changed in this PR
Extends the distributed-type-parameter checks from conditional types to homomorphic mapped types, addressing the same generic-constraint violations described in #63708.
Changes:
- Recognizes mapped-type distributive contexts and normalizes distributed parameters during type comparisons and inference.
- Adds regression coverage for constraint diagnostics, mapped-type inference, and editor hover information.
- Updates compiler and trace baselines.
| File | Description |
|---|---|
tsc/testdata/tests/cases/conformance/types/mapped/isomorphicMappedTypeInference.ts |
Tests inference of ordinary type parameters. |
tsc/testdata/tests/cases/compiler/reverseMappedTypeNestedInference.ts |
Adds nested reverse-mapping inference cases. |
tsc/testdata/tests/cases/compiler/distributedTypeParameters.ts |
Adds mapped and indexed-access constraint cases. |
tsc/testdata/baselines/reference/tsc/generateTrace/generateTrace-creates-static-index-signature-sibling-types-in-a-stable-order.js |
Updates static index-signature trace expectations. |
tsc/testdata/baselines/reference/tsc/generateTrace/generateTrace-creates-index-signature-sibling-types-in-a-stable-order.js |
Updates index-signature trace expectations. |
tsc/testdata/baselines/reference/conformance/isomorphicMappedTypeInference.types |
Records mapped-type inference results. |
tsc/testdata/baselines/reference/conformance/isomorphicMappedTypeInference.symbols |
Records symbols for the inference regression. |
tsc/testdata/baselines/reference/conformance/isomorphicMappedTypeInference.js |
Records JavaScript and declaration output. |
tsc/testdata/baselines/reference/compiler/reverseMappedTypeNestedInference.types |
Records nested inference results. |
tsc/testdata/baselines/reference/compiler/reverseMappedTypeNestedInference.symbols |
Records nested inference symbols. |
tsc/testdata/baselines/reference/compiler/reverseMappedTypeNestedInference.js |
Records nested inference emit output. |
tsc/testdata/baselines/reference/compiler/distributedTypeParameters.types |
Records types for new distribution cases. |
tsc/testdata/baselines/reference/compiler/distributedTypeParameters.symbols |
Records symbols for new distribution cases. |
tsc/testdata/baselines/reference/compiler/distributedTypeParameters.errors.txt |
Records new constraint diagnostics. |
tsc/internal/fourslash/tests/quickInfoDistributedTypeParameter_test.go |
Tests distributed-parameter hover labels. |
tsc/internal/checker/relater.go |
Normalizes mapped types before comparisons. |
tsc/internal/checker/inference.go |
Adjusts inference and indexed-access substitution. |
tsc/internal/checker/checker.go |
Detects mapped distributive contexts and adds normalization helpers. |
Review effort: Balanced
…g index signatures
There was a problem hiding this comment.
🟡 Changes recommended
Mapped-type distribution handling still accepts unsafe generic constraints and rejects valid nested mappings due to incorrect handling of aliased constraints and enclosing distributed parameters.
3 open issues
2 resolved since last review
Review effort: Balanced
| if constraint := ast.SkipTypeParentheses(constraint); ast.IsTypeOperatorNode(constraint) && constraint.AsTypeOperatorNode().Operator == ast.KindKeyOfKeyword { | ||
| typeParameterNode = ast.SkipTypeParentheses(constraint.Type()) |
There was a problem hiding this comment.
This is a gap in the original design of distributed type parameters, which only recognizes a type parameter referenced directly. Conditional types have it too: Alias<T> extends unknown ? Show<T, U> : never distributes over T but reports no error. Mapped types follow the same syntactic rule here, so I am leaving this out of this PR.
Proof:
type Show<A, B extends A> = [A, B] & {};
type Working<A, B extends A> = A extends unknown
? B extends unknown
? Show<A, B> // Error!
: never
: never;
type Alias<P> = P
type Issue<A, B extends A> = Alias<A> extends unknown
? B extends unknown
? Show<A, B> // No error :(
: never
: never;
type X = Issue<0 | 1, 0 | 1>;
// type X = [0, 0] | [0, 1] | [1, 0] | [1, 1] // so this *does* distribute despite the alias| if typeVariable.flags&TypeFlagsTypeParameter == 0 || typeVariable.AsTypeParameter().isDistributed { | ||
| return t | ||
| } | ||
| result := c.getNonDistributedType(t, c.getDistributedTypeFromTypeParameter(typeVariable)) |
| if typeVariable.flags&TypeFlagsTypeParameter == 0 || typeVariable.AsTypeParameter().isDistributed { | ||
| return t |


this is basically the same thing as #64237 + #64307 , just for mapped types.
The distributive mapped types exhibit the very same kind of problem as the one reported originally in #63708
In the resulting tuples the second element should be assignable to the first element but it's clearly not -
1 | 2is not assignable to1or2andunknownis not assignable to0or1. TheShow's constraint should prevent such bogus instantiations but yet we end up with such here without reporting an error.