Skip to content

Fix definitely assignable relation for mapped types #26602

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 22, 2018
Merged
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
3 changes: 3 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11778,6 +11778,9 @@ namespace ts {
}
return Ternary.False;
}
if (relation === definitelyAssignableRelation && isGenericMappedType(source)) {
return Ternary.False;
}
const sourceIsPrimitive = !!(source.flags & TypeFlags.Primitive);
if (relation !== identityRelation) {
source = getApparentType(source);
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/mappedTypeNoTypeNoCrash.types
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
=== tests/cases/compiler/mappedTypeNoTypeNoCrash.ts ===
type T0<T> = ({[K in keyof T]}) extends ({[key in K]: T[K]}) ? number : never;
>T0 : number
>T0 : T0<T>

12 changes: 12 additions & 0 deletions tests/baselines/reference/mappedTypesArraysTuples.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ declare function mapArray<T extends any[]>(arr: T): Mapped<T>;
function acceptMappedArray<T extends any[]>(arr: T) {
acceptArray(mapArray(arr));
}

// Repro from #26163

type Unconstrained<T> = ElementType<Mapped<T>>;
type T1 = Unconstrained<[string, number, boolean]>; // string | number | boolean

type Constrained<T extends any[]> = ElementType<Mapped<T>>;
type T2 = Constrained<[string, number, boolean]>; // string | number | boolean


//// [mappedTypesArraysTuples.js]
Expand Down Expand Up @@ -182,3 +190,7 @@ declare type R2 = ElementType<Mapped<[string, number, boolean]>>;
declare function acceptArray(arr: any[]): void;
declare function mapArray<T extends any[]>(arr: T): Mapped<T>;
declare function acceptMappedArray<T extends any[]>(arr: T): void;
declare type Unconstrained<T> = ElementType<Mapped<T>>;
declare type T1 = Unconstrained<[string, number, boolean]>;
declare type Constrained<T extends any[]> = ElementType<Mapped<T>>;
declare type T2 = Constrained<[string, number, boolean]>;
24 changes: 24 additions & 0 deletions tests/baselines/reference/mappedTypesArraysTuples.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -328,3 +328,27 @@ function acceptMappedArray<T extends any[]>(arr: T) {
>arr : Symbol(arr, Decl(mappedTypesArraysTuples.ts, 81, 44))
}

// Repro from #26163

type Unconstrained<T> = ElementType<Mapped<T>>;
>Unconstrained : Symbol(Unconstrained, Decl(mappedTypesArraysTuples.ts, 83, 1))
>T : Symbol(T, Decl(mappedTypesArraysTuples.ts, 87, 19))
>ElementType : Symbol(ElementType, Decl(mappedTypesArraysTuples.ts, 66, 1))
>Mapped : Symbol(Mapped, Decl(mappedTypesArraysTuples.ts, 70, 59))
>T : Symbol(T, Decl(mappedTypesArraysTuples.ts, 87, 19))

type T1 = Unconstrained<[string, number, boolean]>; // string | number | boolean
>T1 : Symbol(T1, Decl(mappedTypesArraysTuples.ts, 87, 47))
>Unconstrained : Symbol(Unconstrained, Decl(mappedTypesArraysTuples.ts, 83, 1))

type Constrained<T extends any[]> = ElementType<Mapped<T>>;
>Constrained : Symbol(Constrained, Decl(mappedTypesArraysTuples.ts, 88, 51))
>T : Symbol(T, Decl(mappedTypesArraysTuples.ts, 90, 17))
>ElementType : Symbol(ElementType, Decl(mappedTypesArraysTuples.ts, 66, 1))
>Mapped : Symbol(Mapped, Decl(mappedTypesArraysTuples.ts, 70, 59))
>T : Symbol(T, Decl(mappedTypesArraysTuples.ts, 90, 17))

type T2 = Constrained<[string, number, boolean]>; // string | number | boolean
>T2 : Symbol(T2, Decl(mappedTypesArraysTuples.ts, 90, 59))
>Constrained : Symbol(Constrained, Decl(mappedTypesArraysTuples.ts, 88, 51))

14 changes: 14 additions & 0 deletions tests/baselines/reference/mappedTypesArraysTuples.types
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,17 @@ function acceptMappedArray<T extends any[]>(arr: T) {
>arr : T
}

// Repro from #26163

type Unconstrained<T> = ElementType<Mapped<T>>;
>Unconstrained : ElementType<Mapped<T>>

type T1 = Unconstrained<[string, number, boolean]>; // string | number | boolean
>T1 : string | number | boolean

type Constrained<T extends any[]> = ElementType<Mapped<T>>;
>Constrained : ElementType<Mapped<T>>

type T2 = Constrained<[string, number, boolean]>; // string | number | boolean
>T2 : string | number | boolean

Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,11 @@ declare function mapArray<T extends any[]>(arr: T): Mapped<T>;
function acceptMappedArray<T extends any[]>(arr: T) {
acceptArray(mapArray(arr));
}

// Repro from #26163

type Unconstrained<T> = ElementType<Mapped<T>>;
type T1 = Unconstrained<[string, number, boolean]>; // string | number | boolean

type Constrained<T extends any[]> = ElementType<Mapped<T>>;
type T2 = Constrained<[string, number, boolean]>; // string | number | boolean