Skip to content
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

Propagate wildcard types in non-homomorphic mapped types #41622

Merged
merged 4 commits into from
Dec 1, 2020
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
4 changes: 3 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15269,7 +15269,9 @@ namespace ts {
});
}
}
return instantiateAnonymousType(type, mapper);
// If the constraint type of the instantiation is the wildcard type, return the wildcard type.
const result = <MappedType>instantiateAnonymousType(type, mapper);
return getConstraintTypeFromMappedType(result) === wildcardType ? wildcardType : result;
}

function getModifiedReadonlyState(state: boolean, modifiers: MappedTypeModifiers) {
Expand Down
6 changes: 6 additions & 0 deletions tests/baselines/reference/conditionalTypes2.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -326,4 +326,10 @@ tests/cases/conformance/types/conditional/conditionalTypes2.ts(75,12): error TS2
declare function gg<T>(f: (x: Foo3<T>) => void): void;
type Foo3<T> = T extends number ? { n: T } : { x: T };
gg(ff);

// Repro from #41613

type Wat<K extends string> = { x: { y: 0, z: 1 } } extends { x: { [P in K]: 0 } } ? true : false;

type Huh = Wat<"y">; // true

17 changes: 17 additions & 0 deletions tests/baselines/reference/conditionalTypes2.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,12 @@ declare function ff(x: Foo3<string>): void;
declare function gg<T>(f: (x: Foo3<T>) => void): void;
type Foo3<T> = T extends number ? { n: T } : { x: T };
gg(ff);

// Repro from #41613

type Wat<K extends string> = { x: { y: 0, z: 1 } } extends { x: { [P in K]: 0 } } ? true : false;

type Huh = Wat<"y">; // true


//// [conditionalTypes2.js]
Expand Down Expand Up @@ -477,3 +483,14 @@ declare type Foo3<T> = T extends number ? {
} : {
x: T;
};
declare type Wat<K extends string> = {
x: {
y: 0;
z: 1;
};
} extends {
x: {
[P in K]: 0;
};
} ? true : false;
declare type Huh = Wat<"y">;
16 changes: 16 additions & 0 deletions tests/baselines/reference/conditionalTypes2.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -841,3 +841,19 @@ gg(ff);
>gg : Symbol(gg, Decl(conditionalTypes2.ts, 234, 43))
>ff : Symbol(ff, Decl(conditionalTypes2.ts, 230, 2))

// Repro from #41613

type Wat<K extends string> = { x: { y: 0, z: 1 } } extends { x: { [P in K]: 0 } } ? true : false;
>Wat : Symbol(Wat, Decl(conditionalTypes2.ts, 237, 7))
>K : Symbol(K, Decl(conditionalTypes2.ts, 241, 9))
>x : Symbol(x, Decl(conditionalTypes2.ts, 241, 30))
>y : Symbol(y, Decl(conditionalTypes2.ts, 241, 35))
>z : Symbol(z, Decl(conditionalTypes2.ts, 241, 41))
>x : Symbol(x, Decl(conditionalTypes2.ts, 241, 60))
>P : Symbol(P, Decl(conditionalTypes2.ts, 241, 67))
>K : Symbol(K, Decl(conditionalTypes2.ts, 241, 9))

type Huh = Wat<"y">; // true
>Huh : Symbol(Huh, Decl(conditionalTypes2.ts, 241, 97))
>Wat : Symbol(Wat, Decl(conditionalTypes2.ts, 237, 7))

14 changes: 14 additions & 0 deletions tests/baselines/reference/conditionalTypes2.types
Original file line number Diff line number Diff line change
Expand Up @@ -527,3 +527,17 @@ gg(ff);
>gg : <T>(f: (x: Foo3<T>) => void) => void
>ff : (x: { x: string; }) => void

// Repro from #41613

type Wat<K extends string> = { x: { y: 0, z: 1 } } extends { x: { [P in K]: 0 } } ? true : false;
>Wat : Wat<K>
>x : { y: 0; z: 1; }
>y : 0
>z : 1
>x : { [P in K]: 0; }
>true : true
>false : false

type Huh = Wat<"y">; // true
>Huh : true

13 changes: 8 additions & 5 deletions tests/baselines/reference/recursiveMappedTypes.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ tests/cases/conformance/types/mapped/recursiveMappedTypes.ts(11,6): error TS2456
tests/cases/conformance/types/mapped/recursiveMappedTypes.ts(12,11): error TS2313: Type parameter 'K' has a circular constraint.
tests/cases/conformance/types/mapped/recursiveMappedTypes.ts(20,19): error TS2589: Type instantiation is excessively deep and possibly infinite.
tests/cases/conformance/types/mapped/recursiveMappedTypes.ts(66,25): error TS2313: Type parameter 'P' has a circular constraint.
tests/cases/conformance/types/mapped/recursiveMappedTypes.ts(79,1): error TS2615: Type of property '"each"' circularly references itself in mapped type '{ type: never; minimum_count: never; maximum_count: never; collapsable?: never; each: never; }'.
tests/cases/conformance/types/mapped/recursiveMappedTypes.ts(73,5): error TS2502: '"each"' is referenced directly or indirectly in its own type annotation.
tests/cases/conformance/types/mapped/recursiveMappedTypes.ts(73,13): error TS2615: Type of property '"each"' circularly references itself in mapped type '{ [P in keyof ListWidget]: undefined extends ListWidget[P] ? never : P; }'.


==== tests/cases/conformance/types/mapped/recursiveMappedTypes.ts (9 errors) ====
==== tests/cases/conformance/types/mapped/recursiveMappedTypes.ts (10 errors) ====
// Recursive mapped types simply appear empty

type Recurse = {
Expand Down Expand Up @@ -93,20 +94,22 @@ tests/cases/conformance/types/mapped/recursiveMappedTypes.ts(79,1): error TS2615
type Child<T> = { [P in NonOptionalKeys<T>]: T[P] }
~~~~~~~~~~~~~~~~~~
!!! error TS2313: Type parameter 'P' has a circular constraint.
!!! related TS2751 tests/cases/conformance/types/mapped/recursiveMappedTypes.ts:79:1: Circularity originates in type at this location.
!!! related TS2751 tests/cases/conformance/types/mapped/recursiveMappedTypes.ts:73:13: Circularity originates in type at this location.

export interface ListWidget {
"type": "list",
"minimum_count": number,
"maximum_count": number,
"collapsable"?: boolean, //default to false, means all expanded
"each": Child<ListWidget>;
~~~~~~
!!! error TS2502: '"each"' is referenced directly or indirectly in its own type annotation.
~~~~~~~~~~~~~~~~~
!!! error TS2615: Type of property '"each"' circularly references itself in mapped type '{ [P in keyof ListWidget]: undefined extends ListWidget[P] ? never : P; }'.
}

type ListChild = Child<ListWidget>

declare let x: ListChild;
x.type;
~~~~~~
!!! error TS2615: Type of property '"each"' circularly references itself in mapped type '{ type: never; minimum_count: never; maximum_count: never; collapsable?: never; each: never; }'.

2 changes: 1 addition & 1 deletion tests/baselines/reference/recursiveMappedTypes.types
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export interface ListWidget {
>"collapsable" : boolean

"each": Child<ListWidget>;
>"each" : Child<ListWidget>
>"each" : any
}

type ListChild = Child<ListWidget>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,9 @@ declare function ff(x: Foo3<string>): void;
declare function gg<T>(f: (x: Foo3<T>) => void): void;
type Foo3<T> = T extends number ? { n: T } : { x: T };
gg(ff);

// Repro from #41613

type Wat<K extends string> = { x: { y: 0, z: 1 } } extends { x: { [P in K]: 0 } } ? true : false;

type Huh = Wat<"y">; // true