Skip to content

Distinguish between non-distributed and distributed type parameters in mapped types too - #65666

Open
Mateusz Burzyński (Andarist) wants to merge 13 commits into
microsoft:mainfrom
Andarist:mapped-type-parameters-rebased-fresh
Open

Mateusz Burzyński (Andarist) wants to merge 13 commits into
microsoft:mainfrom
Andarist:mapped-type-parameters-rebased-fresh

Conversation

@Andarist

Copy link
Copy Markdown
Contributor

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

type Identity<T> = { [K in keyof T]: T[K] } & unknown
type Show<T, U extends T> = [T, U] & {};

type Cond<A, B extends A> =
  A extends unknown ?
    Show<A, B> :  // Error
    never;

type Mapped<A, B extends A> = {
  [K in keyof A]: Show<A[K], B[K]> // No error?
}

type FishyResult1 = Identity<Mapped<{ foo: 1 } | { foo: 2 }, { foo: 1 } | { foo: 2 }>>
// ^? type FishyResult1 = { foo: [1, 1 | 2]; } | { foo: [2, 1 | 2]; }
type FishyResult2 = Identity<Mapped<{ foo: 0 } | { bar: 1 }, { foo: 0 }>>
// type FishyResult2 = { foo: [0, 0]; } | { bar: [1, unknown]; }
type FishyResult3 = Identity<Mapped<{ foo: 0 } | { bar: 1 }, { foo: 0 } | { bar: 1 }>>
// type FishyResult3 = { foo: [0, unknown]; } | { bar: [1, unknown]; }

In the resulting tuples the second element should be assignable to the first element but it's clearly not - 1 | 2 is not assignable to 1 or 2 and unknown is not assignable to 0 or 1. The Show's constraint should prevent such bogus instantiations but yet we end up with such here without reporting an error.

Copilot AI balanced review requested due to automatic review settings November 25, 2026 11:38
@github-project-automation github-project-automation Bot moved this to Not started in PR Backlog Nov 25, 2026
@typescript-automation typescript-automation Bot added the For Uncommitted Bug PR for untriaged, rejected, closed or missing bug label Nov 25, 2026
@typescript-automation

Copy link
Copy Markdown
Contributor

This PR doesn't have any linked issues. Please open an issue that references this PR. From there we can discuss and prioritise.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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

Comment thread tsc/internal/checker/checker.go Resolved
Comment thread tsc/internal/checker/checker.go Resolved

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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

Comment on lines +23928 to +23929
if constraint := ast.SkipTypeParentheses(constraint); ast.IsTypeOperatorNode(constraint) && constraint.AsTypeOperatorNode().Operator == ast.KindKeyOfKeyword {
typeParameterNode = ast.SkipTypeParentheses(constraint.Type())

@Andarist Mateusz Burzyński (Andarist) Nov 25, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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))
Comment on lines +23977 to +23978
if typeVariable.flags&TypeFlagsTypeParameter == 0 || typeVariable.AsTypeParameter().isDistributed {
return t

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

For Uncommitted Bug PR for untriaged, rejected, closed or missing bug

Projects

Status: Not started

Development

Successfully merging this pull request may close these issues.

2 participants