diff --git a/tsc/internal/checker/checker.go b/tsc/internal/checker/checker.go index 694fc155b8e16..044c532544f9f 100644 --- a/tsc/internal/checker/checker.go +++ b/tsc/internal/checker/checker.go @@ -23162,7 +23162,7 @@ func (c *Checker) getObjectTypeInstantiation(t *Type, m *TypeMapper, alias *Type case target.objectFlags&ObjectFlagsReference != 0: result = c.createDeferredTypeReference(t.Target(), t.AsTypeReference().node, newMapper, newAlias) case target.objectFlags&ObjectFlagsMapped != 0: - result = c.instantiateMappedType(target, newMapper, newAlias) + result = c.instantiateMappedType(target, typeParameters, newMapper, newAlias) default: result = c.instantiateAnonymousType(target, newMapper, newAlias) } @@ -23317,7 +23317,7 @@ func (c *Checker) getHomomorphicTypeVariable(t *Type) *Type { return nil } -func (c *Checker) instantiateMappedType(t *Type, m *TypeMapper, alias *TypeAlias) *Type { +func (c *Checker) instantiateMappedType(t *Type, typeParameters []*Type, m *TypeMapper, alias *TypeAlias) *Type { // For a homomorphic mapped type { [P in keyof T]: X }, where T is some type variable, the mapping // operation depends on T as follows: // * If T is a primitive type no mapping is performed and the result is simply T. @@ -23347,7 +23347,7 @@ func (c *Checker) instantiateMappedType(t *Type, m *TypeMapper, alias *TypeAlias return c.getIntersectionType(core.Map(s.Types(), instantiateConstituent)) } } - return c.instantiateAnonymousType(t, prependTypeMapping(typeVariable, s, m), nil) + return c.getMappedTypeConstituentInstantiation(t, typeParameters, typeVariable, s, m) } if typeVariable != nil { mappedTypeVariable := c.instantiateType(typeVariable, m) @@ -23362,6 +23362,28 @@ func (c *Checker) instantiateMappedType(t *Type, m *TypeMapper, alias *TypeAlias return c.instantiateAnonymousType(t, m, alias) } +// Returns the instantiation of the homomorphic mapped type t for a single constituent of its type variable. +// The result is cached under the key getObjectTypeInstantiation computes when the result is itself instantiated, +// such that instantiating it again yields the same type instead of a structurally identical duplicate. The key +// is independent of any alias of the enclosing instantiation, since that alias doesn't propagate to constituents. +func (c *Checker) getMappedTypeConstituentInstantiation(t *Type, typeParameters []*Type, typeVariable *Type, s *Type, m *TypeMapper) *Type { + mapper := prependTypeMapping(typeVariable, s, m) + if mapper.Map(typeVariable) != s { + // When s is itself a type parameter mapped by m, the merged mapper maps the type variable to the mapping + // of s. The result then differs from the instantiation for its key, so it isn't cached. + return c.instantiateAnonymousType(t, mapper, nil) + } + alias := c.instantiateTypeAlias(t.alias, mapper) + key := getTypeInstantiationKey(core.Map(typeParameters, mapper.Map), alias, false /*singleSignature*/) + data := t.AsObjectType() + result := data.instantiations[key] + if result == nil { + result = c.instantiateAnonymousType(t, mapper, alias) + data.instantiations[key] = result + } + return result +} + func (c *Checker) hasArrayOrTypeTypeConstraint(typeVariable *Type) bool { constraint := c.getConstraintOfTypeParameter(typeVariable) return constraint != nil && everyType(constraint, c.isArrayOrTupleType) diff --git a/tsc/testdata/baselines/reference/compiler/declarationEmitUnserializableTypes.types b/tsc/testdata/baselines/reference/compiler/declarationEmitUnserializableTypes.types index 91ac8787ea5e1..ca3440c44141b 100644 --- a/tsc/testdata/baselines/reference/compiler/declarationEmitUnserializableTypes.types +++ b/tsc/testdata/baselines/reference/compiler/declarationEmitUnserializableTypes.types @@ -102,7 +102,7 @@ type show = { [k in keyof t]: t[k] } & unknown; >show : { [k in keyof t]: t[k]; } type resolve<$, k extends keyof $> = show<{ ->resolve : { [p in keyof $[k]]: $[k][p] extends "ref" ? any extends infer T_1 ? { [k_1 in keyof T_1]: T_1[k_1]; } : never : $[k][p]; } extends infer T ? { [k_1 in keyof T]: T[k_1]; } : never +>resolve : { [p in keyof $[k]]: $[k][p] extends "ref" ? any : $[k][p]; } extends infer T ? { [k_1 in keyof T]: T[k_1]; } : never [p in keyof $[k]]: $[k][p] extends "ref" ? resolve<$, k> : $[k][p] }>; diff --git a/tsc/testdata/baselines/reference/compiler/homomorphicMappedTypeReinstantiation.symbols b/tsc/testdata/baselines/reference/compiler/homomorphicMappedTypeReinstantiation.symbols new file mode 100644 index 0000000000000..a7adb0b2a87a1 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/homomorphicMappedTypeReinstantiation.symbols @@ -0,0 +1,156 @@ +//// [tests/cases/compiler/homomorphicMappedTypeReinstantiation.ts] //// + +=== homomorphicMappedTypeReinstantiation.ts === +// https://github.com/microsoft/TypeScript/issues/65560 + +declare function either(a: A, b: B): A | B; +>either : Symbol(either, Decl(homomorphicMappedTypeReinstantiation.ts, 0, 0)) +>A : Symbol(A, Decl(homomorphicMappedTypeReinstantiation.ts, 2, 24)) +>B : Symbol(B, Decl(homomorphicMappedTypeReinstantiation.ts, 2, 26)) +>a : Symbol(a, Decl(homomorphicMappedTypeReinstantiation.ts, 2, 30)) +>A : Symbol(A, Decl(homomorphicMappedTypeReinstantiation.ts, 2, 24)) +>b : Symbol(b, Decl(homomorphicMappedTypeReinstantiation.ts, 2, 35)) +>B : Symbol(B, Decl(homomorphicMappedTypeReinstantiation.ts, 2, 26)) +>A : Symbol(A, Decl(homomorphicMappedTypeReinstantiation.ts, 2, 24)) +>B : Symbol(B, Decl(homomorphicMappedTypeReinstantiation.ts, 2, 26)) + +type Options = Partial<{ verbose: boolean }>; +>Options : Symbol(Options, Decl(homomorphicMappedTypeReinstantiation.ts, 2, 49)) +>Partial : Symbol(Partial, Decl(lib.es5.d.ts, --, --)) +>verbose : Symbol(verbose, Decl(homomorphicMappedTypeReinstantiation.ts, 4, 24)) + +declare const options: (Options | string)[]; +>options : Symbol(options, Decl(homomorphicMappedTypeReinstantiation.ts, 5, 13)) +>Options : Symbol(Options, Decl(homomorphicMappedTypeReinstantiation.ts, 2, 49)) + +const r1 = either(options[0], options.map(value => value)[0]); +>r1 : Symbol(r1, Decl(homomorphicMappedTypeReinstantiation.ts, 6, 5)) +>either : Symbol(either, Decl(homomorphicMappedTypeReinstantiation.ts, 0, 0)) +>options : Symbol(options, Decl(homomorphicMappedTypeReinstantiation.ts, 5, 13)) +>options.map : Symbol(Array.map, Decl(lib.es5.d.ts, --, --)) +>options : Symbol(options, Decl(homomorphicMappedTypeReinstantiation.ts, 5, 13)) +>map : Symbol(Array.map, Decl(lib.es5.d.ts, --, --)) +>value : Symbol(value, Decl(homomorphicMappedTypeReinstantiation.ts, 6, 42)) +>value : Symbol(value, Decl(homomorphicMappedTypeReinstantiation.ts, 6, 42)) + +type ReadonlyOptions = Readonly<{ verbose: boolean }>; +>ReadonlyOptions : Symbol(ReadonlyOptions, Decl(homomorphicMappedTypeReinstantiation.ts, 6, 62)) +>Readonly : Symbol(Readonly, Decl(lib.es5.d.ts, --, --)) +>verbose : Symbol(verbose, Decl(homomorphicMappedTypeReinstantiation.ts, 8, 33)) + +declare const readonlyOptions: (ReadonlyOptions | string)[]; +>readonlyOptions : Symbol(readonlyOptions, Decl(homomorphicMappedTypeReinstantiation.ts, 9, 13)) +>ReadonlyOptions : Symbol(ReadonlyOptions, Decl(homomorphicMappedTypeReinstantiation.ts, 6, 62)) + +const r2 = either(readonlyOptions[0], readonlyOptions.map(value => value)[0]); +>r2 : Symbol(r2, Decl(homomorphicMappedTypeReinstantiation.ts, 10, 5)) +>either : Symbol(either, Decl(homomorphicMappedTypeReinstantiation.ts, 0, 0)) +>readonlyOptions : Symbol(readonlyOptions, Decl(homomorphicMappedTypeReinstantiation.ts, 9, 13)) +>readonlyOptions.map : Symbol(Array.map, Decl(lib.es5.d.ts, --, --)) +>readonlyOptions : Symbol(readonlyOptions, Decl(homomorphicMappedTypeReinstantiation.ts, 9, 13)) +>map : Symbol(Array.map, Decl(lib.es5.d.ts, --, --)) +>value : Symbol(value, Decl(homomorphicMappedTypeReinstantiation.ts, 10, 58)) +>value : Symbol(value, Decl(homomorphicMappedTypeReinstantiation.ts, 10, 58)) + +type Nullable = { [K in keyof T]: T[K] | null }; +>Nullable : Symbol(Nullable, Decl(homomorphicMappedTypeReinstantiation.ts, 10, 78)) +>T : Symbol(T, Decl(homomorphicMappedTypeReinstantiation.ts, 12, 14)) +>K : Symbol(K, Decl(homomorphicMappedTypeReinstantiation.ts, 12, 22)) +>T : Symbol(T, Decl(homomorphicMappedTypeReinstantiation.ts, 12, 14)) +>T : Symbol(T, Decl(homomorphicMappedTypeReinstantiation.ts, 12, 14)) +>K : Symbol(K, Decl(homomorphicMappedTypeReinstantiation.ts, 12, 22)) + +type NullableOptions = Nullable<{ verbose: boolean }>; +>NullableOptions : Symbol(NullableOptions, Decl(homomorphicMappedTypeReinstantiation.ts, 12, 51)) +>Nullable : Symbol(Nullable, Decl(homomorphicMappedTypeReinstantiation.ts, 10, 78)) +>verbose : Symbol(verbose, Decl(homomorphicMappedTypeReinstantiation.ts, 13, 33)) + +declare const nullableOptions: (NullableOptions | string)[]; +>nullableOptions : Symbol(nullableOptions, Decl(homomorphicMappedTypeReinstantiation.ts, 14, 13)) +>NullableOptions : Symbol(NullableOptions, Decl(homomorphicMappedTypeReinstantiation.ts, 12, 51)) + +const r3 = either(nullableOptions[0], nullableOptions.map(value => value)[0]); +>r3 : Symbol(r3, Decl(homomorphicMappedTypeReinstantiation.ts, 15, 5)) +>either : Symbol(either, Decl(homomorphicMappedTypeReinstantiation.ts, 0, 0)) +>nullableOptions : Symbol(nullableOptions, Decl(homomorphicMappedTypeReinstantiation.ts, 14, 13)) +>nullableOptions.map : Symbol(Array.map, Decl(lib.es5.d.ts, --, --)) +>nullableOptions : Symbol(nullableOptions, Decl(homomorphicMappedTypeReinstantiation.ts, 14, 13)) +>map : Symbol(Array.map, Decl(lib.es5.d.ts, --, --)) +>value : Symbol(value, Decl(homomorphicMappedTypeReinstantiation.ts, 15, 58)) +>value : Symbol(value, Decl(homomorphicMappedTypeReinstantiation.ts, 15, 58)) + +declare const unionOptions: (Partial<{ a: boolean } | { b: string }> | string)[]; +>unionOptions : Symbol(unionOptions, Decl(homomorphicMappedTypeReinstantiation.ts, 17, 13)) +>Partial : Symbol(Partial, Decl(lib.es5.d.ts, --, --)) +>a : Symbol(a, Decl(homomorphicMappedTypeReinstantiation.ts, 17, 38)) +>b : Symbol(b, Decl(homomorphicMappedTypeReinstantiation.ts, 17, 55)) + +const r4 = either(unionOptions[0], unionOptions.map(value => value)[0]); +>r4 : Symbol(r4, Decl(homomorphicMappedTypeReinstantiation.ts, 18, 5)) +>either : Symbol(either, Decl(homomorphicMappedTypeReinstantiation.ts, 0, 0)) +>unionOptions : Symbol(unionOptions, Decl(homomorphicMappedTypeReinstantiation.ts, 17, 13)) +>unionOptions.map : Symbol(Array.map, Decl(lib.es5.d.ts, --, --)) +>unionOptions : Symbol(unionOptions, Decl(homomorphicMappedTypeReinstantiation.ts, 17, 13)) +>map : Symbol(Array.map, Decl(lib.es5.d.ts, --, --)) +>value : Symbol(value, Decl(homomorphicMappedTypeReinstantiation.ts, 18, 52)) +>value : Symbol(value, Decl(homomorphicMappedTypeReinstantiation.ts, 18, 52)) + +type AliasedUnionOptions = Partial<{ a: boolean } | { b: string }>; +>AliasedUnionOptions : Symbol(AliasedUnionOptions, Decl(homomorphicMappedTypeReinstantiation.ts, 18, 72)) +>Partial : Symbol(Partial, Decl(lib.es5.d.ts, --, --)) +>a : Symbol(a, Decl(homomorphicMappedTypeReinstantiation.ts, 20, 36)) +>b : Symbol(b, Decl(homomorphicMappedTypeReinstantiation.ts, 20, 53)) + +declare const aliasedUnionOptions: (AliasedUnionOptions | string)[]; +>aliasedUnionOptions : Symbol(aliasedUnionOptions, Decl(homomorphicMappedTypeReinstantiation.ts, 21, 13)) +>AliasedUnionOptions : Symbol(AliasedUnionOptions, Decl(homomorphicMappedTypeReinstantiation.ts, 18, 72)) + +const r5 = either(aliasedUnionOptions[0], aliasedUnionOptions.map(value => value)[0]); +>r5 : Symbol(r5, Decl(homomorphicMappedTypeReinstantiation.ts, 22, 5)) +>either : Symbol(either, Decl(homomorphicMappedTypeReinstantiation.ts, 0, 0)) +>aliasedUnionOptions : Symbol(aliasedUnionOptions, Decl(homomorphicMappedTypeReinstantiation.ts, 21, 13)) +>aliasedUnionOptions.map : Symbol(Array.map, Decl(lib.es5.d.ts, --, --)) +>aliasedUnionOptions : Symbol(aliasedUnionOptions, Decl(homomorphicMappedTypeReinstantiation.ts, 21, 13)) +>map : Symbol(Array.map, Decl(lib.es5.d.ts, --, --)) +>value : Symbol(value, Decl(homomorphicMappedTypeReinstantiation.ts, 22, 66)) +>value : Symbol(value, Decl(homomorphicMappedTypeReinstantiation.ts, 22, 66)) + +type Named = { verbose: boolean }; +>Named : Symbol(Named, Decl(homomorphicMappedTypeReinstantiation.ts, 22, 86)) +>verbose : Symbol(verbose, Decl(homomorphicMappedTypeReinstantiation.ts, 24, 14)) + +type A1 = Partial; +>A1 : Symbol(A1, Decl(homomorphicMappedTypeReinstantiation.ts, 24, 34)) +>Partial : Symbol(Partial, Decl(lib.es5.d.ts, --, --)) +>Named : Symbol(Named, Decl(homomorphicMappedTypeReinstantiation.ts, 22, 86)) + +type A2 = Partial; +>A2 : Symbol(A2, Decl(homomorphicMappedTypeReinstantiation.ts, 25, 25)) +>Partial : Symbol(Partial, Decl(lib.es5.d.ts, --, --)) +>Named : Symbol(Named, Decl(homomorphicMappedTypeReinstantiation.ts, 22, 86)) + +declare const a1: A1; +>a1 : Symbol(a1, Decl(homomorphicMappedTypeReinstantiation.ts, 27, 13)) +>A1 : Symbol(A1, Decl(homomorphicMappedTypeReinstantiation.ts, 24, 34)) + +declare const a2: A2; +>a2 : Symbol(a2, Decl(homomorphicMappedTypeReinstantiation.ts, 28, 13)) +>A2 : Symbol(A2, Decl(homomorphicMappedTypeReinstantiation.ts, 25, 25)) + +declare const a3: Partial; +>a3 : Symbol(a3, Decl(homomorphicMappedTypeReinstantiation.ts, 29, 13)) +>Partial : Symbol(Partial, Decl(lib.es5.d.ts, --, --)) +>Named : Symbol(Named, Decl(homomorphicMappedTypeReinstantiation.ts, 22, 86)) + +const r6 = either(a1, a2); +>r6 : Symbol(r6, Decl(homomorphicMappedTypeReinstantiation.ts, 30, 5)) +>either : Symbol(either, Decl(homomorphicMappedTypeReinstantiation.ts, 0, 0)) +>a1 : Symbol(a1, Decl(homomorphicMappedTypeReinstantiation.ts, 27, 13)) +>a2 : Symbol(a2, Decl(homomorphicMappedTypeReinstantiation.ts, 28, 13)) + +const r7 = either(a1, a3); +>r7 : Symbol(r7, Decl(homomorphicMappedTypeReinstantiation.ts, 31, 5)) +>either : Symbol(either, Decl(homomorphicMappedTypeReinstantiation.ts, 0, 0)) +>a1 : Symbol(a1, Decl(homomorphicMappedTypeReinstantiation.ts, 27, 13)) +>a3 : Symbol(a3, Decl(homomorphicMappedTypeReinstantiation.ts, 29, 13)) + diff --git a/tsc/testdata/baselines/reference/compiler/homomorphicMappedTypeReinstantiation.types b/tsc/testdata/baselines/reference/compiler/homomorphicMappedTypeReinstantiation.types new file mode 100644 index 0000000000000..7602aa9c92155 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/homomorphicMappedTypeReinstantiation.types @@ -0,0 +1,165 @@ +//// [tests/cases/compiler/homomorphicMappedTypeReinstantiation.ts] //// + +=== homomorphicMappedTypeReinstantiation.ts === +// https://github.com/microsoft/TypeScript/issues/65560 + +declare function either(a: A, b: B): A | B; +>either : (a: A, b: B) => A | B +>a : A +>b : B + +type Options = Partial<{ verbose: boolean }>; +>Options : Partial<{ verbose: boolean; }> +>verbose : boolean + +declare const options: (Options | string)[]; +>options : (string | Partial<{ verbose: boolean; }>)[] + +const r1 = either(options[0], options.map(value => value)[0]); +>r1 : string | Partial<{ verbose: boolean; }> +>either(options[0], options.map(value => value)[0]) : string | Partial<{ verbose: boolean; }> +>either : (a: A, b: B) => A | B +>options[0] : string | Partial<{ verbose: boolean; }> +>options : (string | Partial<{ verbose: boolean; }>)[] +>0 : 0 +>options.map(value => value)[0] : string | Partial<{ verbose: boolean; }> +>options.map(value => value) : (string | Partial<{ verbose: boolean; }>)[] +>options.map : (callbackfn: (value: string | Partial<{ verbose: boolean; }>, index: number, array: (string | Partial<{ verbose: boolean; }>)[]) => U, thisArg?: any) => U[] +>options : (string | Partial<{ verbose: boolean; }>)[] +>map : (callbackfn: (value: string | Partial<{ verbose: boolean; }>, index: number, array: (string | Partial<{ verbose: boolean; }>)[]) => U, thisArg?: any) => U[] +>value => value : (value: string | Partial<{ verbose: boolean; }>) => string | Partial<{ verbose: boolean; }> +>value : string | Partial<{ verbose: boolean; }> +>value : string | Partial<{ verbose: boolean; }> +>0 : 0 + +type ReadonlyOptions = Readonly<{ verbose: boolean }>; +>ReadonlyOptions : Readonly<{ verbose: boolean; }> +>verbose : boolean + +declare const readonlyOptions: (ReadonlyOptions | string)[]; +>readonlyOptions : (string | Readonly<{ verbose: boolean; }>)[] + +const r2 = either(readonlyOptions[0], readonlyOptions.map(value => value)[0]); +>r2 : string | Readonly<{ verbose: boolean; }> +>either(readonlyOptions[0], readonlyOptions.map(value => value)[0]) : string | Readonly<{ verbose: boolean; }> +>either : (a: A, b: B) => A | B +>readonlyOptions[0] : string | Readonly<{ verbose: boolean; }> +>readonlyOptions : (string | Readonly<{ verbose: boolean; }>)[] +>0 : 0 +>readonlyOptions.map(value => value)[0] : string | Readonly<{ verbose: boolean; }> +>readonlyOptions.map(value => value) : (string | Readonly<{ verbose: boolean; }>)[] +>readonlyOptions.map : (callbackfn: (value: string | Readonly<{ verbose: boolean; }>, index: number, array: (string | Readonly<{ verbose: boolean; }>)[]) => U, thisArg?: any) => U[] +>readonlyOptions : (string | Readonly<{ verbose: boolean; }>)[] +>map : (callbackfn: (value: string | Readonly<{ verbose: boolean; }>, index: number, array: (string | Readonly<{ verbose: boolean; }>)[]) => U, thisArg?: any) => U[] +>value => value : (value: string | Readonly<{ verbose: boolean; }>) => string | Readonly<{ verbose: boolean; }> +>value : string | Readonly<{ verbose: boolean; }> +>value : string | Readonly<{ verbose: boolean; }> +>0 : 0 + +type Nullable = { [K in keyof T]: T[K] | null }; +>Nullable : Nullable + +type NullableOptions = Nullable<{ verbose: boolean }>; +>NullableOptions : Nullable<{ verbose: boolean; }> +>verbose : boolean + +declare const nullableOptions: (NullableOptions | string)[]; +>nullableOptions : (string | Nullable<{ verbose: boolean; }>)[] + +const r3 = either(nullableOptions[0], nullableOptions.map(value => value)[0]); +>r3 : string | Nullable<{ verbose: boolean; }> +>either(nullableOptions[0], nullableOptions.map(value => value)[0]) : string | Nullable<{ verbose: boolean; }> +>either : (a: A, b: B) => A | B +>nullableOptions[0] : string | Nullable<{ verbose: boolean; }> +>nullableOptions : (string | Nullable<{ verbose: boolean; }>)[] +>0 : 0 +>nullableOptions.map(value => value)[0] : string | Nullable<{ verbose: boolean; }> +>nullableOptions.map(value => value) : (string | Nullable<{ verbose: boolean; }>)[] +>nullableOptions.map : (callbackfn: (value: string | Nullable<{ verbose: boolean; }>, index: number, array: (string | Nullable<{ verbose: boolean; }>)[]) => U, thisArg?: any) => U[] +>nullableOptions : (string | Nullable<{ verbose: boolean; }>)[] +>map : (callbackfn: (value: string | Nullable<{ verbose: boolean; }>, index: number, array: (string | Nullable<{ verbose: boolean; }>)[]) => U, thisArg?: any) => U[] +>value => value : (value: string | Nullable<{ verbose: boolean; }>) => string | Nullable<{ verbose: boolean; }> +>value : string | Nullable<{ verbose: boolean; }> +>value : string | Nullable<{ verbose: boolean; }> +>0 : 0 + +declare const unionOptions: (Partial<{ a: boolean } | { b: string }> | string)[]; +>unionOptions : (string | Partial<{ a: boolean; } | { b: string; }>)[] +>a : boolean +>b : string + +const r4 = either(unionOptions[0], unionOptions.map(value => value)[0]); +>r4 : string | Partial<{ a: boolean; } | { b: string; }> +>either(unionOptions[0], unionOptions.map(value => value)[0]) : string | Partial<{ a: boolean; } | { b: string; }> +>either : (a: A, b: B) => A | B +>unionOptions[0] : string | Partial<{ a: boolean; } | { b: string; }> +>unionOptions : (string | Partial<{ a: boolean; } | { b: string; }>)[] +>0 : 0 +>unionOptions.map(value => value)[0] : string | Partial<{ a: boolean; } | { b: string; }> +>unionOptions.map(value => value) : (string | Partial<{ a: boolean; } | { b: string; }>)[] +>unionOptions.map : (callbackfn: (value: string | Partial<{ a: boolean; } | { b: string; }>, index: number, array: (string | Partial<{ a: boolean; } | { b: string; }>)[]) => U, thisArg?: any) => U[] +>unionOptions : (string | Partial<{ a: boolean; } | { b: string; }>)[] +>map : (callbackfn: (value: string | Partial<{ a: boolean; } | { b: string; }>, index: number, array: (string | Partial<{ a: boolean; } | { b: string; }>)[]) => U, thisArg?: any) => U[] +>value => value : (value: string | Partial<{ a: boolean; } | { b: string; }>) => string | Partial<{ a: boolean; } | { b: string; }> +>value : string | Partial<{ a: boolean; } | { b: string; }> +>value : string | Partial<{ a: boolean; } | { b: string; }> +>0 : 0 + +type AliasedUnionOptions = Partial<{ a: boolean } | { b: string }>; +>AliasedUnionOptions : AliasedUnionOptions +>a : boolean +>b : string + +declare const aliasedUnionOptions: (AliasedUnionOptions | string)[]; +>aliasedUnionOptions : (string | AliasedUnionOptions)[] + +const r5 = either(aliasedUnionOptions[0], aliasedUnionOptions.map(value => value)[0]); +>r5 : string | AliasedUnionOptions +>either(aliasedUnionOptions[0], aliasedUnionOptions.map(value => value)[0]) : string | AliasedUnionOptions +>either : (a: A, b: B) => A | B +>aliasedUnionOptions[0] : string | AliasedUnionOptions +>aliasedUnionOptions : (string | AliasedUnionOptions)[] +>0 : 0 +>aliasedUnionOptions.map(value => value)[0] : string | AliasedUnionOptions +>aliasedUnionOptions.map(value => value) : (string | AliasedUnionOptions)[] +>aliasedUnionOptions.map : (callbackfn: (value: string | AliasedUnionOptions, index: number, array: (string | AliasedUnionOptions)[]) => U, thisArg?: any) => U[] +>aliasedUnionOptions : (string | AliasedUnionOptions)[] +>map : (callbackfn: (value: string | AliasedUnionOptions, index: number, array: (string | AliasedUnionOptions)[]) => U, thisArg?: any) => U[] +>value => value : (value: string | AliasedUnionOptions) => string | AliasedUnionOptions +>value : string | AliasedUnionOptions +>value : string | AliasedUnionOptions +>0 : 0 + +type Named = { verbose: boolean }; +>Named : Named +>verbose : boolean + +type A1 = Partial; +>A1 : Partial + +type A2 = Partial; +>A2 : Partial + +declare const a1: A1; +>a1 : Partial + +declare const a2: A2; +>a2 : Partial + +declare const a3: Partial; +>a3 : Partial + +const r6 = either(a1, a2); +>r6 : Partial +>either(a1, a2) : Partial +>either : (a: A, b: B) => A | B +>a1 : Partial +>a2 : Partial + +const r7 = either(a1, a3); +>r7 : Partial +>either(a1, a3) : Partial +>either : (a: A, b: B) => A | B +>a1 : Partial +>a3 : Partial + diff --git a/tsc/testdata/baselines/reference/compiler/homomorphicMappedTypeReinstantiationOverlappingParameters.symbols b/tsc/testdata/baselines/reference/compiler/homomorphicMappedTypeReinstantiationOverlappingParameters.symbols new file mode 100644 index 0000000000000..274cf58490aec --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/homomorphicMappedTypeReinstantiationOverlappingParameters.symbols @@ -0,0 +1,122 @@ +//// [tests/cases/compiler/homomorphicMappedTypeReinstantiationOverlappingParameters.ts] //// + +=== homomorphicMappedTypeReinstantiationOverlappingParameters.ts === +// Self-referential homomorphic mapped types whose type variable is instantiated to another of their own type parameters. + +type F = { [K in keyof T]: F }; +>F : Symbol(F, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 0, 0)) +>T : Symbol(T, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 2, 7)) +>U : Symbol(U, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 2, 9)) +>K : Symbol(K, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 2, 18)) +>T : Symbol(T, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 2, 7)) +>F : Symbol(F, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 0, 0)) +>U : Symbol(U, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 2, 9)) + +declare const f: F<{ a: 1 }, number>; +>f : Symbol(f, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 3, 13)) +>F : Symbol(F, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 0, 0)) +>a : Symbol(a, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 3, 20)) + +const f1 = f.a; +>f1 : Symbol(f1, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 4, 5)) +>f.a : Symbol(a, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 3, 20)) +>f : Symbol(f, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 3, 13)) +>a : Symbol(a, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 3, 20)) + +const f2: F = "x"; +>f2 : Symbol(f2, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 5, 5)) +>F : Symbol(F, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 0, 0)) + +type X = { x: 1 }; +>X : Symbol(X, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 5, 34)) +>x : Symbol(x, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 7, 10)) + +type R = { +>R : Symbol(R, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 7, 18)) +>T : Symbol(T, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 8, 7)) +>U : Symbol(U, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 8, 9)) + + [K in keyof T]: { +>K : Symbol(K, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 9, 5)) +>T : Symbol(T, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 8, 7)) + + rec: R; +>rec : Symbol(rec, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 9, 21)) +>R : Symbol(R, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 7, 18)) +>U : Symbol(U, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 8, 9)) +>U : Symbol(U, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 8, 9)) +>X : Symbol(X, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 5, 34)) + + direct: R; +>direct : Symbol(direct, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 10, 25)) +>R : Symbol(R, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 7, 18)) +>U : Symbol(U, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 8, 9)) +>X : Symbol(X, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 5, 34)) +>U : Symbol(U, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 8, 9)) +>X : Symbol(X, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 5, 34)) + } +}; +declare const r: R<{ a: 1 }, { b: 2 }>; +>r : Symbol(r, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 14, 13)) +>R : Symbol(R, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 7, 18)) +>a : Symbol(a, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 14, 20)) +>b : Symbol(b, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 14, 30)) + +const r1 = r.a.rec; +>r1 : Symbol(r1, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 15, 5)) +>r.a.rec : Symbol(rec, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 9, 21)) +>r.a : Symbol(a, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 14, 20)) +>r : Symbol(r, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 14, 13)) +>a : Symbol(a, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 14, 20)) +>rec : Symbol(rec, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 9, 21)) + +const r2 = r.a.direct; +>r2 : Symbol(r2, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 16, 5)) +>r.a.direct : Symbol(direct, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 10, 25)) +>r.a : Symbol(a, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 14, 20)) +>r : Symbol(r, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 14, 13)) +>a : Symbol(a, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 14, 20)) +>direct : Symbol(direct, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 10, 25)) + +type G = { [K in keyof T]: { next: G; value: T[K] } }; +>G : Symbol(G, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 16, 22)) +>T : Symbol(T, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 18, 7)) +>U : Symbol(U, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 18, 9)) +>K : Symbol(K, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 18, 18)) +>T : Symbol(T, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 18, 7)) +>next : Symbol(next, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 18, 34)) +>G : Symbol(G, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 16, 22)) +>U : Symbol(U, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 18, 9)) +>value : Symbol(value, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 18, 56)) +>T : Symbol(T, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 18, 7)) +>K : Symbol(K, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 18, 18)) + +declare const g: G<{ a: 1 }, number>; +>g : Symbol(g, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 19, 13)) +>G : Symbol(G, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 16, 22)) +>a : Symbol(a, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 19, 20)) + +const g1 = g.a.next; +>g1 : Symbol(g1, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 20, 5)) +>g.a.next : Symbol(next, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 18, 34)) +>g.a : Symbol(a, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 19, 20)) +>g : Symbol(g, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 19, 13)) +>a : Symbol(a, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 19, 20)) +>next : Symbol(next, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 18, 34)) + +declare const g2: G; +>g2 : Symbol(g2, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 21, 13)) +>G : Symbol(G, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 16, 22)) + +const g3 = g2[0].value; +>g3 : Symbol(g3, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 22, 5)) +>g2[0].value : Symbol(value, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 18, 56)) +>g2 : Symbol(g2, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 21, 13)) +>value : Symbol(value, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 18, 56)) + +const g4 = g2.length; +>g4 : Symbol(g4, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 23, 5)) +>g2.length : Symbol(Array.length, Decl(lib.es5.d.ts, --, --)) +>g2 : Symbol(g2, Decl(homomorphicMappedTypeReinstantiationOverlappingParameters.ts, 21, 13)) +>length : Symbol(Array.length, Decl(lib.es5.d.ts, --, --)) + diff --git a/tsc/testdata/baselines/reference/compiler/homomorphicMappedTypeReinstantiationOverlappingParameters.types b/tsc/testdata/baselines/reference/compiler/homomorphicMappedTypeReinstantiationOverlappingParameters.types new file mode 100644 index 0000000000000..861e47b5df3f1 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/homomorphicMappedTypeReinstantiationOverlappingParameters.types @@ -0,0 +1,92 @@ +//// [tests/cases/compiler/homomorphicMappedTypeReinstantiationOverlappingParameters.ts] //// + +=== homomorphicMappedTypeReinstantiationOverlappingParameters.ts === +// Self-referential homomorphic mapped types whose type variable is instantiated to another of their own type parameters. + +type F = { [K in keyof T]: F }; +>F : F + +declare const f: F<{ a: 1 }, number>; +>f : F<{ a: 1; }, number> +>a : 1 + +const f1 = f.a; +>f1 : string +>f.a : string +>f : F<{ a: 1; }, number> +>a : string + +const f2: F = "x"; +>f2 : string +>"x" : "x" + +type X = { x: 1 }; +>X : X +>x : 1 + +type R = { +>R : R + + [K in keyof T]: { + rec: R; +>rec : R + + direct: R; +>direct : R + } +}; +declare const r: R<{ a: 1 }, { b: 2 }>; +>r : R<{ a: 1; }, { b: 2; }> +>a : 1 +>b : 2 + +const r1 = r.a.rec; +>r1 : R +>r.a.rec : R +>r.a : { rec: R; direct: R; } +>r : R<{ a: 1; }, { b: 2; }> +>a : { rec: R; direct: R; } +>rec : R + +const r2 = r.a.direct; +>r2 : R +>r.a.direct : R +>r.a : { rec: R; direct: R; } +>r : R<{ a: 1; }, { b: 2; }> +>a : { rec: R; direct: R; } +>direct : R + +type G = { [K in keyof T]: { next: G; value: T[K] } }; +>G : G +>next : G +>value : T[K] + +declare const g: G<{ a: 1 }, number>; +>g : G<{ a: 1; }, number> +>a : 1 + +const g1 = g.a.next; +>g1 : { next: any; value: string; }[] +>g.a.next : { next: any; value: string; }[] +>g.a : { next: { next: any; value: string; }[]; value: 1; } +>g : G<{ a: 1; }, number> +>a : { next: { next: any; value: string; }[]; value: 1; } +>next : { next: any; value: string; }[] + +declare const g2: G; +>g2 : { next: any; value: string; }[] + +const g3 = g2[0].value; +>g3 : string +>g2[0].value : string +>g2[0] : { next: any[]; value: string; } +>g2 : { next: any; value: string; }[] +>0 : 0 +>value : string + +const g4 = g2.length; +>g4 : number +>g2.length : number +>g2 : { next: any; value: string; }[] +>length : number + diff --git a/tsc/testdata/tests/cases/compiler/homomorphicMappedTypeReinstantiation.ts b/tsc/testdata/tests/cases/compiler/homomorphicMappedTypeReinstantiation.ts new file mode 100644 index 0000000000000..0a71dc3cdd887 --- /dev/null +++ b/tsc/testdata/tests/cases/compiler/homomorphicMappedTypeReinstantiation.ts @@ -0,0 +1,35 @@ +// @strict: true +// @noEmit: true + +// https://github.com/microsoft/TypeScript/issues/65560 + +declare function either(a: A, b: B): A | B; + +type Options = Partial<{ verbose: boolean }>; +declare const options: (Options | string)[]; +const r1 = either(options[0], options.map(value => value)[0]); + +type ReadonlyOptions = Readonly<{ verbose: boolean }>; +declare const readonlyOptions: (ReadonlyOptions | string)[]; +const r2 = either(readonlyOptions[0], readonlyOptions.map(value => value)[0]); + +type Nullable = { [K in keyof T]: T[K] | null }; +type NullableOptions = Nullable<{ verbose: boolean }>; +declare const nullableOptions: (NullableOptions | string)[]; +const r3 = either(nullableOptions[0], nullableOptions.map(value => value)[0]); + +declare const unionOptions: (Partial<{ a: boolean } | { b: string }> | string)[]; +const r4 = either(unionOptions[0], unionOptions.map(value => value)[0]); + +type AliasedUnionOptions = Partial<{ a: boolean } | { b: string }>; +declare const aliasedUnionOptions: (AliasedUnionOptions | string)[]; +const r5 = either(aliasedUnionOptions[0], aliasedUnionOptions.map(value => value)[0]); + +type Named = { verbose: boolean }; +type A1 = Partial; +type A2 = Partial; +declare const a1: A1; +declare const a2: A2; +declare const a3: Partial; +const r6 = either(a1, a2); +const r7 = either(a1, a3); diff --git a/tsc/testdata/tests/cases/compiler/homomorphicMappedTypeReinstantiationLargeUnion.ts b/tsc/testdata/tests/cases/compiler/homomorphicMappedTypeReinstantiationLargeUnion.ts new file mode 100644 index 0000000000000..04e6f959b958d --- /dev/null +++ b/tsc/testdata/tests/cases/compiler/homomorphicMappedTypeReinstantiationLargeUnion.ts @@ -0,0 +1,16 @@ +// @strict: true +// @noEmit: true +// @target: es2022 +// @noTypesAndSymbols: true + +// https://github.com/microsoft/TypeScript/issues/65560 + +type Digit = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9; +type Id = `${0 | 1 | 2 | 3}${Digit}${Digit}${Digit}`; // 4,000 ids + +type Options = Partial<{ verbose: boolean }>; + +type State = { [K in Id]?: { id: K; value: number } } & { options?: Options }; + +declare const state: State; +export const keys = Object.entries(state).map((entry) => entry[0]); diff --git a/tsc/testdata/tests/cases/compiler/homomorphicMappedTypeReinstantiationOverlappingParameters.ts b/tsc/testdata/tests/cases/compiler/homomorphicMappedTypeReinstantiationOverlappingParameters.ts new file mode 100644 index 0000000000000..153b864101312 --- /dev/null +++ b/tsc/testdata/tests/cases/compiler/homomorphicMappedTypeReinstantiationOverlappingParameters.ts @@ -0,0 +1,27 @@ +// @strict: true +// @noEmit: true + +// Self-referential homomorphic mapped types whose type variable is instantiated to another of their own type parameters. + +type F = { [K in keyof T]: F }; +declare const f: F<{ a: 1 }, number>; +const f1 = f.a; +const f2: F = "x"; + +type X = { x: 1 }; +type R = { + [K in keyof T]: { + rec: R; + direct: R; + } +}; +declare const r: R<{ a: 1 }, { b: 2 }>; +const r1 = r.a.rec; +const r2 = r.a.direct; + +type G = { [K in keyof T]: { next: G; value: T[K] } }; +declare const g: G<{ a: 1 }, number>; +const g1 = g.a.next; +declare const g2: G; +const g3 = g2[0].value; +const g4 = g2.length;