-
Notifications
You must be signed in to change notification settings - Fork 12.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds 'Awaited' type alias and updates to Promise.all/race/allSettled/any
- Loading branch information
Showing
33 changed files
with
1,079 additions
and
125 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
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
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
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
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,52 @@ | ||
tests/cases/compiler/awaitedType.ts(18,12): error TS2589: Type instantiation is excessively deep and possibly infinite. | ||
tests/cases/compiler/awaitedType.ts(22,12): error TS2589: Type instantiation is excessively deep and possibly infinite. | ||
|
||
|
||
==== tests/cases/compiler/awaitedType.ts (2 errors) ==== | ||
type T1 = Awaited<number>; | ||
type T2 = Awaited<Promise<number>>; | ||
type T3 = Awaited<number | Promise<number>>; | ||
type T4 = Awaited<number | Promise<string>>; | ||
type T5 = Awaited<{ then: number }>; | ||
type T6 = Awaited<{ then(): void }>; // never (non-promise "thenable") | ||
type T7 = Awaited<{ then(x: number): void }>; // never (non-promise "thenable") | ||
type T8 = Awaited<{ then(x: () => void): void }>; // unknown | ||
type T9 = Awaited<any>; | ||
type T10 = Awaited<never>; | ||
type T11 = Awaited<unknown>; | ||
type T12 = Awaited<Promise<Promise<number>>>; | ||
type T13 = _Expect<Awaited<Promise<Promise<number>> | string | null>, /*expected*/ string | number | null>; // otherwise just prints T13 in types tests, which isn't very helpful | ||
type T14 = _Expect<Awaited<Promise<Promise<number>> | string | undefined>, /*expected*/ string | number | undefined>; // otherwise just prints T14 in types tests, which isn't very helpful | ||
type T15 = _Expect<Awaited<Promise<Promise<number>> | string | null | undefined>, /*expected*/ string | number | null | undefined>; // otherwise just prints T15 in types tests, which isn't very helpful | ||
|
||
interface BadPromise { then(cb: (value: BadPromise) => void): void; } | ||
type T16 = Awaited<BadPromise>; // error | ||
~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS2589: Type instantiation is excessively deep and possibly infinite. | ||
|
||
interface BadPromise1 { then(cb: (value: BadPromise2) => void): void; } | ||
interface BadPromise2 { then(cb: (value: BadPromise1) => void): void; } | ||
type T17 = Awaited<BadPromise1>; // error | ||
~~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS2589: Type instantiation is excessively deep and possibly infinite. | ||
|
||
// https://github.com/microsoft/TypeScript/issues/33562 | ||
type MaybePromise<T> = T | Promise<T> | PromiseLike<T> | ||
declare function MaybePromise<T>(value: T): MaybePromise<T>; | ||
|
||
async function main() { | ||
let aaa: number; | ||
let bbb: string; | ||
[ | ||
aaa, | ||
bbb, | ||
] = await Promise.all([ | ||
MaybePromise(1), | ||
MaybePromise('2'), | ||
MaybePromise(true), | ||
]) | ||
} | ||
|
||
// helps with tests where '.types' just prints out the type alias name | ||
type _Expect<TActual extends TExpected, TExpected> = TActual; | ||
|
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,58 @@ | ||
//// [awaitedType.ts] | ||
type T1 = Awaited<number>; | ||
type T2 = Awaited<Promise<number>>; | ||
type T3 = Awaited<number | Promise<number>>; | ||
type T4 = Awaited<number | Promise<string>>; | ||
type T5 = Awaited<{ then: number }>; | ||
type T6 = Awaited<{ then(): void }>; // never (non-promise "thenable") | ||
type T7 = Awaited<{ then(x: number): void }>; // never (non-promise "thenable") | ||
type T8 = Awaited<{ then(x: () => void): void }>; // unknown | ||
type T9 = Awaited<any>; | ||
type T10 = Awaited<never>; | ||
type T11 = Awaited<unknown>; | ||
type T12 = Awaited<Promise<Promise<number>>>; | ||
type T13 = _Expect<Awaited<Promise<Promise<number>> | string | null>, /*expected*/ string | number | null>; // otherwise just prints T13 in types tests, which isn't very helpful | ||
type T14 = _Expect<Awaited<Promise<Promise<number>> | string | undefined>, /*expected*/ string | number | undefined>; // otherwise just prints T14 in types tests, which isn't very helpful | ||
type T15 = _Expect<Awaited<Promise<Promise<number>> | string | null | undefined>, /*expected*/ string | number | null | undefined>; // otherwise just prints T15 in types tests, which isn't very helpful | ||
|
||
interface BadPromise { then(cb: (value: BadPromise) => void): void; } | ||
type T16 = Awaited<BadPromise>; // error | ||
|
||
interface BadPromise1 { then(cb: (value: BadPromise2) => void): void; } | ||
interface BadPromise2 { then(cb: (value: BadPromise1) => void): void; } | ||
type T17 = Awaited<BadPromise1>; // error | ||
|
||
// https://github.com/microsoft/TypeScript/issues/33562 | ||
type MaybePromise<T> = T | Promise<T> | PromiseLike<T> | ||
declare function MaybePromise<T>(value: T): MaybePromise<T>; | ||
|
||
async function main() { | ||
let aaa: number; | ||
let bbb: string; | ||
[ | ||
aaa, | ||
bbb, | ||
] = await Promise.all([ | ||
MaybePromise(1), | ||
MaybePromise('2'), | ||
MaybePromise(true), | ||
]) | ||
} | ||
|
||
// helps with tests where '.types' just prints out the type alias name | ||
type _Expect<TActual extends TExpected, TExpected> = TActual; | ||
|
||
|
||
//// [awaitedType.js] | ||
async function main() { | ||
let aaa; | ||
let bbb; | ||
[ | ||
aaa, | ||
bbb, | ||
] = await Promise.all([ | ||
MaybePromise(1), | ||
MaybePromise('2'), | ||
MaybePromise(true), | ||
]); | ||
} |
Oops, something went wrong.