-
Notifications
You must be signed in to change notification settings - Fork 12.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17521 from Microsoft/deferLookupTypeResolution
Defer indexed access type resolution
- Loading branch information
Showing
8 changed files
with
433 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
//// [deferredLookupTypeResolution.ts] | ||
// Repro from #17456 | ||
|
||
type StringContains<S extends string, L extends string> = ( | ||
{ [K in S]: 'true' } & | ||
{ [key: string]: 'false' } | ||
)[L] | ||
|
||
type ObjectHasKey<O, L extends string> = StringContains<keyof O, L> | ||
|
||
type First<T> = ObjectHasKey<T, '0'>; // Should be deferred | ||
|
||
type T1 = ObjectHasKey<{ a: string }, 'a'>; // 'true' | ||
type T2 = ObjectHasKey<{ a: string }, 'b'>; // 'false' | ||
|
||
// Verify that mapped type isn't eagerly resolved in type-to-string operation | ||
|
||
declare function f1<A extends string, B extends string>(a: A, b: B): { [P in A | B]: any }; | ||
|
||
function f2<A extends string>(a: A) { | ||
return f1(a, 'x'); | ||
} | ||
|
||
function f3(x: 'a' | 'b') { | ||
return f2(x); | ||
} | ||
|
||
|
||
//// [deferredLookupTypeResolution.js] | ||
"use strict"; | ||
// Repro from #17456 | ||
function f2(a) { | ||
return f1(a, 'x'); | ||
} | ||
function f3(x) { | ||
return f2(x); | ||
} | ||
|
||
|
||
//// [deferredLookupTypeResolution.d.ts] | ||
declare type StringContains<S extends string, L extends string> = ({ | ||
[K in S]: 'true'; | ||
} & { | ||
[key: string]: 'false'; | ||
})[L]; | ||
declare type ObjectHasKey<O, L extends string> = StringContains<keyof O, L>; | ||
declare type First<T> = ObjectHasKey<T, '0'>; | ||
declare type T1 = ObjectHasKey<{ | ||
a: string; | ||
}, 'a'>; | ||
declare type T2 = ObjectHasKey<{ | ||
a: string; | ||
}, 'b'>; | ||
declare function f1<A extends string, B extends string>(a: A, b: B): { | ||
[P in A | B]: any; | ||
}; | ||
declare function f2<A extends string>(a: A): { | ||
[P in A | "x"]: any; | ||
}; | ||
declare function f3(x: 'a' | 'b'): { | ||
a: any; | ||
b: any; | ||
x: any; | ||
}; |
76 changes: 76 additions & 0 deletions
76
tests/baselines/reference/deferredLookupTypeResolution.symbols
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
=== tests/cases/compiler/deferredLookupTypeResolution.ts === | ||
// Repro from #17456 | ||
|
||
type StringContains<S extends string, L extends string> = ( | ||
>StringContains : Symbol(StringContains, Decl(deferredLookupTypeResolution.ts, 0, 0)) | ||
>S : Symbol(S, Decl(deferredLookupTypeResolution.ts, 2, 20)) | ||
>L : Symbol(L, Decl(deferredLookupTypeResolution.ts, 2, 37)) | ||
|
||
{ [K in S]: 'true' } & | ||
>K : Symbol(K, Decl(deferredLookupTypeResolution.ts, 3, 7)) | ||
>S : Symbol(S, Decl(deferredLookupTypeResolution.ts, 2, 20)) | ||
|
||
{ [key: string]: 'false' } | ||
>key : Symbol(key, Decl(deferredLookupTypeResolution.ts, 4, 7)) | ||
|
||
)[L] | ||
>L : Symbol(L, Decl(deferredLookupTypeResolution.ts, 2, 37)) | ||
|
||
type ObjectHasKey<O, L extends string> = StringContains<keyof O, L> | ||
>ObjectHasKey : Symbol(ObjectHasKey, Decl(deferredLookupTypeResolution.ts, 5, 6)) | ||
>O : Symbol(O, Decl(deferredLookupTypeResolution.ts, 7, 18)) | ||
>L : Symbol(L, Decl(deferredLookupTypeResolution.ts, 7, 20)) | ||
>StringContains : Symbol(StringContains, Decl(deferredLookupTypeResolution.ts, 0, 0)) | ||
>O : Symbol(O, Decl(deferredLookupTypeResolution.ts, 7, 18)) | ||
>L : Symbol(L, Decl(deferredLookupTypeResolution.ts, 7, 20)) | ||
|
||
type First<T> = ObjectHasKey<T, '0'>; // Should be deferred | ||
>First : Symbol(First, Decl(deferredLookupTypeResolution.ts, 7, 67)) | ||
>T : Symbol(T, Decl(deferredLookupTypeResolution.ts, 9, 11)) | ||
>ObjectHasKey : Symbol(ObjectHasKey, Decl(deferredLookupTypeResolution.ts, 5, 6)) | ||
>T : Symbol(T, Decl(deferredLookupTypeResolution.ts, 9, 11)) | ||
|
||
type T1 = ObjectHasKey<{ a: string }, 'a'>; // 'true' | ||
>T1 : Symbol(T1, Decl(deferredLookupTypeResolution.ts, 9, 37)) | ||
>ObjectHasKey : Symbol(ObjectHasKey, Decl(deferredLookupTypeResolution.ts, 5, 6)) | ||
>a : Symbol(a, Decl(deferredLookupTypeResolution.ts, 11, 24)) | ||
|
||
type T2 = ObjectHasKey<{ a: string }, 'b'>; // 'false' | ||
>T2 : Symbol(T2, Decl(deferredLookupTypeResolution.ts, 11, 43)) | ||
>ObjectHasKey : Symbol(ObjectHasKey, Decl(deferredLookupTypeResolution.ts, 5, 6)) | ||
>a : Symbol(a, Decl(deferredLookupTypeResolution.ts, 12, 24)) | ||
|
||
// Verify that mapped type isn't eagerly resolved in type-to-string operation | ||
|
||
declare function f1<A extends string, B extends string>(a: A, b: B): { [P in A | B]: any }; | ||
>f1 : Symbol(f1, Decl(deferredLookupTypeResolution.ts, 12, 43)) | ||
>A : Symbol(A, Decl(deferredLookupTypeResolution.ts, 16, 20)) | ||
>B : Symbol(B, Decl(deferredLookupTypeResolution.ts, 16, 37)) | ||
>a : Symbol(a, Decl(deferredLookupTypeResolution.ts, 16, 56)) | ||
>A : Symbol(A, Decl(deferredLookupTypeResolution.ts, 16, 20)) | ||
>b : Symbol(b, Decl(deferredLookupTypeResolution.ts, 16, 61)) | ||
>B : Symbol(B, Decl(deferredLookupTypeResolution.ts, 16, 37)) | ||
>P : Symbol(P, Decl(deferredLookupTypeResolution.ts, 16, 72)) | ||
>A : Symbol(A, Decl(deferredLookupTypeResolution.ts, 16, 20)) | ||
>B : Symbol(B, Decl(deferredLookupTypeResolution.ts, 16, 37)) | ||
|
||
function f2<A extends string>(a: A) { | ||
>f2 : Symbol(f2, Decl(deferredLookupTypeResolution.ts, 16, 91)) | ||
>A : Symbol(A, Decl(deferredLookupTypeResolution.ts, 18, 12)) | ||
>a : Symbol(a, Decl(deferredLookupTypeResolution.ts, 18, 30)) | ||
>A : Symbol(A, Decl(deferredLookupTypeResolution.ts, 18, 12)) | ||
|
||
return f1(a, 'x'); | ||
>f1 : Symbol(f1, Decl(deferredLookupTypeResolution.ts, 12, 43)) | ||
>a : Symbol(a, Decl(deferredLookupTypeResolution.ts, 18, 30)) | ||
} | ||
|
||
function f3(x: 'a' | 'b') { | ||
>f3 : Symbol(f3, Decl(deferredLookupTypeResolution.ts, 20, 1)) | ||
>x : Symbol(x, Decl(deferredLookupTypeResolution.ts, 22, 12)) | ||
|
||
return f2(x); | ||
>f2 : Symbol(f2, Decl(deferredLookupTypeResolution.ts, 16, 91)) | ||
>x : Symbol(x, Decl(deferredLookupTypeResolution.ts, 22, 12)) | ||
} | ||
|
79 changes: 79 additions & 0 deletions
79
tests/baselines/reference/deferredLookupTypeResolution.types
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
=== tests/cases/compiler/deferredLookupTypeResolution.ts === | ||
// Repro from #17456 | ||
|
||
type StringContains<S extends string, L extends string> = ( | ||
>StringContains : ({ [K in S]: "true"; } & { [key: string]: "false"; })[L] | ||
>S : S | ||
>L : L | ||
|
||
{ [K in S]: 'true' } & | ||
>K : K | ||
>S : S | ||
|
||
{ [key: string]: 'false' } | ||
>key : string | ||
|
||
)[L] | ||
>L : L | ||
|
||
type ObjectHasKey<O, L extends string> = StringContains<keyof O, L> | ||
>ObjectHasKey : ({ [K in S]: "true"; } & { [key: string]: "false"; })[L] | ||
>O : O | ||
>L : L | ||
>StringContains : ({ [K in S]: "true"; } & { [key: string]: "false"; })[L] | ||
>O : O | ||
>L : L | ||
|
||
type First<T> = ObjectHasKey<T, '0'>; // Should be deferred | ||
>First : ({ [K in S]: "true"; } & { [key: string]: "false"; })["0"] | ||
>T : T | ||
>ObjectHasKey : ({ [K in S]: "true"; } & { [key: string]: "false"; })[L] | ||
>T : T | ||
|
||
type T1 = ObjectHasKey<{ a: string }, 'a'>; // 'true' | ||
>T1 : "true" | ||
>ObjectHasKey : ({ [K in S]: "true"; } & { [key: string]: "false"; })[L] | ||
>a : string | ||
|
||
type T2 = ObjectHasKey<{ a: string }, 'b'>; // 'false' | ||
>T2 : "false" | ||
>ObjectHasKey : ({ [K in S]: "true"; } & { [key: string]: "false"; })[L] | ||
>a : string | ||
|
||
// Verify that mapped type isn't eagerly resolved in type-to-string operation | ||
|
||
declare function f1<A extends string, B extends string>(a: A, b: B): { [P in A | B]: any }; | ||
>f1 : <A extends string, B extends string>(a: A, b: B) => { [P in A | B]: any; } | ||
>A : A | ||
>B : B | ||
>a : A | ||
>A : A | ||
>b : B | ||
>B : B | ||
>P : P | ||
>A : A | ||
>B : B | ||
|
||
function f2<A extends string>(a: A) { | ||
>f2 : <A extends string>(a: A) => { [P in A | B]: any; } | ||
>A : A | ||
>a : A | ||
>A : A | ||
|
||
return f1(a, 'x'); | ||
>f1(a, 'x') : { [P in A | B]: any; } | ||
>f1 : <A extends string, B extends string>(a: A, b: B) => { [P in A | B]: any; } | ||
>a : A | ||
>'x' : "x" | ||
} | ||
|
||
function f3(x: 'a' | 'b') { | ||
>f3 : (x: "a" | "b") => { a: any; b: any; x: any; } | ||
>x : "a" | "b" | ||
|
||
return f2(x); | ||
>f2(x) : { a: any; b: any; x: any; } | ||
>f2 : <A extends string>(a: A) => { [P in A | B]: any; } | ||
>x : "a" | "b" | ||
} | ||
|
31 changes: 31 additions & 0 deletions
31
tests/baselines/reference/deferredLookupTypeResolution2.errors.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
tests/cases/compiler/deferredLookupTypeResolution2.ts(14,13): error TS2536: Type '({ [K in S]: "true"; } & { [key: string]: "false"; })["1"]' cannot be used to index type '{ true: "true"; }'. | ||
tests/cases/compiler/deferredLookupTypeResolution2.ts(19,21): error TS2536: Type '({ true: "otherwise"; } & { [k: string]: "true"; })[({ [K in S]: "true"; } & { [key: string]: "false"; })["1"]]' cannot be used to index type '{ true: "true"; }'. | ||
|
||
|
||
==== tests/cases/compiler/deferredLookupTypeResolution2.ts (2 errors) ==== | ||
// Repro from #17456 | ||
|
||
type StringContains<S extends string, L extends string> = ({ [K in S]: 'true' } & { [key: string]: 'false'})[L]; | ||
|
||
type ObjectHasKey<O, L extends string> = StringContains<keyof O, L>; | ||
|
||
type A<T> = ObjectHasKey<T, '0'>; | ||
|
||
type B = ObjectHasKey<[string, number], '1'>; // "true" | ||
type C = ObjectHasKey<[string, number], '2'>; // "false" | ||
type D = A<[string]>; // "true" | ||
|
||
// Error, "false" not handled | ||
type E<T> = { true: 'true' }[ObjectHasKey<T, '1'>]; | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS2536: Type '({ [K in S]: "true"; } & { [key: string]: "false"; })["1"]' cannot be used to index type '{ true: "true"; }'. | ||
|
||
type Juxtapose<T> = ({ true: 'otherwise' } & { [k: string]: 'true' })[ObjectHasKey<T, '1'>]; | ||
|
||
// Error, "otherwise" is missing | ||
type DeepError<T> = { true: 'true' }[Juxtapose<T>]; | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS2536: Type '({ true: "otherwise"; } & { [k: string]: "true"; })[({ [K in S]: "true"; } & { [key: string]: "false"; })["1"]]' cannot be used to index type '{ true: "true"; }'. | ||
|
||
type DeepOK<T> = { true: 'true', otherwise: 'false' }[Juxtapose<T>]; | ||
|
Oops, something went wrong.