Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22447,7 +22447,11 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
const sourceIsPrimitive = !!(sourceFlags & TypeFlags.Primitive);
if (relation !== identityRelation) {
const originalSource = source;
source = getApparentType(source);
if (!(originalSource.flags & TypeFlags.Union) && source.flags & TypeFlags.Union && (result = unionOrIntersectionRelatedTo(source, target, reportErrors, intersectionState))) {
return result;
}
sourceFlags = source.flags;
}
else if (isGenericMappedType(source)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ const myFirstFunction = <T extends FirstInterface | SecondInterface>(param1: T)
>3 : 3

mySecondFunction(newParam)
>mySecondFunction(newParam) : { commonProperty: number; otherProperty: number; }
>mySecondFunction(newParam) : T & { otherProperty: number; }
>mySecondFunction : <T_1 extends { commonProperty: number; otherProperty: number; }>(newParam: T_1) => T_1
>newParam : (FirstInterface | SecondInterface) & { otherProperty: number; }
>newParam : T & { otherProperty: number; }
}

const mySecondFunction = <T extends { commonProperty: number, otherProperty: number }>(newParam: T) => {
Expand Down
12 changes: 8 additions & 4 deletions tests/baselines/reference/keyofAndIndexedAccessErrors.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ keyofAndIndexedAccessErrors.ts(103,9): error TS2322: Type 'Extract<keyof T, stri
'Extract<keyof T, string>' is assignable to the constraint of type 'K', but 'K' could be instantiated with a different subtype of constraint 'string'.
Type 'string & keyof T' is not assignable to type 'K'.
'string & keyof T' is assignable to the constraint of type 'K', but 'K' could be instantiated with a different subtype of constraint 'string'.
Type 'string' is not assignable to type 'K'.
'string' is assignable to the constraint of type 'K', but 'K' could be instantiated with a different subtype of constraint 'string'.
Type 'String & string' is not assignable to type 'K'.
'String & string' is assignable to the constraint of type 'K', but 'K' could be instantiated with a different subtype of constraint 'string'.
Type 'string' is not assignable to type 'K'.
'string' is assignable to the constraint of type 'K', but 'K' could be instantiated with a different subtype of constraint 'string'.
keyofAndIndexedAccessErrors.ts(105,9): error TS2322: Type 'T[Extract<keyof T, string>]' is not assignable to type 'T[K]'.
Type 'Extract<keyof T, string>' is not assignable to type 'K'.
'Extract<keyof T, string>' is assignable to the constraint of type 'K', but 'K' could be instantiated with a different subtype of constraint 'string'.
Expand Down Expand Up @@ -249,8 +251,10 @@ keyofAndIndexedAccessErrors.ts(165,5): error TS2322: Type 'number' is not assign
!!! error TS2322: 'Extract<keyof T, string>' is assignable to the constraint of type 'K', but 'K' could be instantiated with a different subtype of constraint 'string'.
!!! error TS2322: Type 'string & keyof T' is not assignable to type 'K'.
!!! error TS2322: 'string & keyof T' is assignable to the constraint of type 'K', but 'K' could be instantiated with a different subtype of constraint 'string'.
!!! error TS2322: Type 'string' is not assignable to type 'K'.
!!! error TS2322: 'string' is assignable to the constraint of type 'K', but 'K' could be instantiated with a different subtype of constraint 'string'.
!!! error TS2322: Type 'String & string' is not assignable to type 'K'.
!!! error TS2322: 'String & string' is assignable to the constraint of type 'K', but 'K' could be instantiated with a different subtype of constraint 'string'.
!!! error TS2322: Type 'string' is not assignable to type 'K'.
!!! error TS2322: 'string' is assignable to the constraint of type 'K', but 'K' could be instantiated with a different subtype of constraint 'string'.
t[key] = tk; // ok, T[K] ==> T[keyof T]
tk = t[key]; // error, T[keyof T] =/=> T[K]
~~
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//// [tests/cases/compiler/mappedTypeUnionArrayTupleConstraint.ts] ////

=== mappedTypeUnionArrayTupleConstraint.ts ===
// https://github.com/microsoft/TypeScript/issues/56018

type Renamed = readonly ({ [k: PropertyKey]: string } | undefined)[];
>Renamed : Symbol(Renamed, Decl(mappedTypeUnionArrayTupleConstraint.ts, 0, 0))
>k : Symbol(k, Decl(mappedTypeUnionArrayTupleConstraint.ts, 2, 28))
>PropertyKey : Symbol(PropertyKey, Decl(lib.es5.d.ts, --, --))

type Foo<T extends readonly (PropertyKey | undefined)[] | Renamed> =
>Foo : Symbol(Foo, Decl(mappedTypeUnionArrayTupleConstraint.ts, 2, 69))
>T : Symbol(T, Decl(mappedTypeUnionArrayTupleConstraint.ts, 4, 9))
>PropertyKey : Symbol(PropertyKey, Decl(lib.es5.d.ts, --, --))
>Renamed : Symbol(Renamed, Decl(mappedTypeUnionArrayTupleConstraint.ts, 0, 0))

T extends Renamed ? GetKeys<Required<T>> : Required<T>;
>T : Symbol(T, Decl(mappedTypeUnionArrayTupleConstraint.ts, 4, 9))
>Renamed : Symbol(Renamed, Decl(mappedTypeUnionArrayTupleConstraint.ts, 0, 0))
>GetKeys : Symbol(GetKeys, Decl(mappedTypeUnionArrayTupleConstraint.ts, 5, 57))
>Required : Symbol(Required, Decl(lib.es5.d.ts, --, --))
>T : Symbol(T, Decl(mappedTypeUnionArrayTupleConstraint.ts, 4, 9))
>Required : Symbol(Required, Decl(lib.es5.d.ts, --, --))
>T : Symbol(T, Decl(mappedTypeUnionArrayTupleConstraint.ts, 4, 9))

type GetKeys<R extends Renamed> = { [K in keyof R]: keyof R[K] };
>GetKeys : Symbol(GetKeys, Decl(mappedTypeUnionArrayTupleConstraint.ts, 5, 57))
>R : Symbol(R, Decl(mappedTypeUnionArrayTupleConstraint.ts, 7, 13))
>Renamed : Symbol(Renamed, Decl(mappedTypeUnionArrayTupleConstraint.ts, 0, 0))
>K : Symbol(K, Decl(mappedTypeUnionArrayTupleConstraint.ts, 7, 37))
>R : Symbol(R, Decl(mappedTypeUnionArrayTupleConstraint.ts, 7, 13))
>R : Symbol(R, Decl(mappedTypeUnionArrayTupleConstraint.ts, 7, 13))
>K : Symbol(K, Decl(mappedTypeUnionArrayTupleConstraint.ts, 7, 37))

// usage
type A = Foo<["a"?]>;
>A : Symbol(A, Decl(mappedTypeUnionArrayTupleConstraint.ts, 7, 65))
>Foo : Symbol(Foo, Decl(mappedTypeUnionArrayTupleConstraint.ts, 2, 69))

type B = Foo<[{ a?: "b" }]>;
>B : Symbol(B, Decl(mappedTypeUnionArrayTupleConstraint.ts, 10, 21))
>Foo : Symbol(Foo, Decl(mappedTypeUnionArrayTupleConstraint.ts, 2, 69))
>a : Symbol(a, Decl(mappedTypeUnionArrayTupleConstraint.ts, 11, 15))

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//// [tests/cases/compiler/mappedTypeUnionArrayTupleConstraint.ts] ////

=== mappedTypeUnionArrayTupleConstraint.ts ===
// https://github.com/microsoft/TypeScript/issues/56018

type Renamed = readonly ({ [k: PropertyKey]: string } | undefined)[];
>Renamed : readonly ({ [k: string]: string; [k: number]: string; [k: symbol]: string; } | undefined)[]
>k : PropertyKey

type Foo<T extends readonly (PropertyKey | undefined)[] | Renamed> =
>Foo : Foo<T>

T extends Renamed ? GetKeys<Required<T>> : Required<T>;

type GetKeys<R extends Renamed> = { [K in keyof R]: keyof R[K] };
>GetKeys : GetKeys<R>

// usage
type A = Foo<["a"?]>;
>A : ["a"]

type B = Foo<[{ a?: "b" }]>;
>B : ["a"]
>a : "b" | undefined

Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mappedTypeUnionConstrainTupleTreatedAsArrayLike.ts(9,9): error TS2322: Type 'HomomorphicMappedType<T>' is not assignable to type 'any[]'.
The type 'readonly [boolean]' is 'readonly' and cannot be assigned to the mutable type 'any[]'.


==== mappedTypeUnionConstrainTupleTreatedAsArrayLike.ts (1 errors) ====
Expand All @@ -13,6 +14,7 @@ mappedTypeUnionConstrainTupleTreatedAsArrayLike.ts(9,9): error TS2322: Type 'Hom
const arr: any[] = [] as HomomorphicMappedType<T> // error
~~~
!!! error TS2322: Type 'HomomorphicMappedType<T>' is not assignable to type 'any[]'.
!!! error TS2322: The type 'readonly [boolean]' is 'readonly' and cannot be assigned to the mutable type 'any[]'.
const arr2: readonly any[] = [] as HomomorphicMappedType<T>
}

15 changes: 15 additions & 0 deletions tests/cases/compiler/mappedTypeUnionArrayTupleConstraint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// @strict: true
// @noEmit: true

// https://github.com/microsoft/TypeScript/issues/56018

type Renamed = readonly ({ [k: PropertyKey]: string } | undefined)[];

type Foo<T extends readonly (PropertyKey | undefined)[] | Renamed> =
T extends Renamed ? GetKeys<Required<T>> : Required<T>;

type GetKeys<R extends Renamed> = { [K in keyof R]: keyof R[K] };

// usage
type A = Foo<["a"?]>;
type B = Foo<[{ a?: "b" }]>;