Skip to content

Commit 5bdae61

Browse files
committed
Use Awaited<T> with 'await'
1 parent 46ce88c commit 5bdae61

8 files changed

+192
-89
lines changed

src/compiler/checker.ts

+168-67
Large diffs are not rendered by default.

src/lib/es5.d.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -1445,11 +1445,13 @@ interface Promise<T> {
14451445
*/
14461446
type Awaited<T> =
14471447
T extends null | undefined ? T : // special case for `null | undefined` when not in `--noImplicitAny` mode
1448-
T extends { then(onfulfilled: infer F): any } ? // thenable, extracts the first argument to `then()`
1449-
F extends ((value: infer V) => any) ? // if the argument to `then` is callable, extracts the argument
1450-
Awaited<V> : // recursively unwrap the value
1451-
never : // the argument to `then` was not callable.
1452-
T; // non-thenable
1448+
T extends object ? // `await` only unwraps object types with a callable then. Non-object types are not unwrapped.
1449+
T extends { then(onfulfilled: infer F): any } ? // thenable, extracts the first argument to `then()`
1450+
F extends ((value: infer V) => any) ? // if the argument to `then` is callable, extracts the argument
1451+
Awaited<V> : // recursively unwrap the value
1452+
never : // the argument to `then` was not callable.
1453+
T : // argument was not an object
1454+
T; // non-thenable
14531455

14541456
interface ArrayLike<T> {
14551457
readonly length: number;

tests/baselines/reference/asyncArrowFunctionCapturesThis_es2017.types

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ class C {
66
>method : () => void
77

88
var fn = async () => await this;
9-
>fn : () => Promise<this>
10-
>async () => await this : () => Promise<this>
11-
>await this : this
9+
>fn : () => Promise<Awaited<this>>
10+
>async () => await this : () => Promise<Awaited<this>>
11+
>await this : Awaited<this>
1212
>this : this
1313
}
1414
}

tests/baselines/reference/asyncArrowFunctionCapturesThis_es5.types

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ class C {
66
>method : () => void
77

88
var fn = async () => await this;
9-
>fn : () => Promise<this>
10-
>async () => await this : () => Promise<this>
11-
>await this : this
9+
>fn : () => Promise<Awaited<this>>
10+
>async () => await this : () => Promise<Awaited<this>>
11+
>await this : Awaited<this>
1212
>this : this
1313
}
1414
}

tests/baselines/reference/asyncArrowFunctionCapturesThis_es6.types

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ class C {
66
>method : () => void
77

88
var fn = async () => await this;
9-
>fn : () => Promise<this>
10-
>async () => await this : () => Promise<this>
11-
>await this : this
9+
>fn : () => Promise<Awaited<this>>
10+
>async () => await this : () => Promise<Awaited<this>>
11+
>await this : Awaited<this>
1212
>this : this
1313
}
1414
}

tests/baselines/reference/doNotElaborateAssignabilityToTypeParameters.errors.txt

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
tests/cases/compiler/doNotElaborateAssignabilityToTypeParameters.ts(3,3): error TS2322: Type 'T | Yadda' is not assignable to type 'T'.
2-
'T' could be instantiated with an arbitrary type which could be unrelated to 'T | Yadda'.
1+
tests/cases/compiler/doNotElaborateAssignabilityToTypeParameters.ts(3,3): error TS2322: Type 'Yadda | Awaited<T>' is not assignable to type 'T'.
2+
'T' could be instantiated with an arbitrary type which could be unrelated to 'Yadda | Awaited<T>'.
33

44

55
==== tests/cases/compiler/doNotElaborateAssignabilityToTypeParameters.ts (1 errors) ====
66
async function foo<T>(x: T): Promise<T> {
77
let yaddable = await getXOrYadda(x);
88
return yaddable;
99
~~~~~~~~~~~~~~~~
10-
!!! error TS2322: Type 'T | Yadda' is not assignable to type 'T'.
11-
!!! error TS2322: 'T' could be instantiated with an arbitrary type which could be unrelated to 'T | Yadda'.
10+
!!! error TS2322: Type 'Yadda | Awaited<T>' is not assignable to type 'T'.
11+
!!! error TS2322: 'T' could be instantiated with an arbitrary type which could be unrelated to 'Yadda | Awaited<T>'.
1212
}
1313

1414
interface Yadda {

tests/baselines/reference/doNotElaborateAssignabilityToTypeParameters.types

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ async function foo<T>(x: T): Promise<T> {
44
>x : T
55

66
let yaddable = await getXOrYadda(x);
7-
>yaddable : T | Yadda
8-
>await getXOrYadda(x) : T | Yadda
7+
>yaddable : Yadda | Awaited<T>
8+
>await getXOrYadda(x) : Yadda | Awaited<T>
99
>getXOrYadda(x) : T | Yadda
1010
>getXOrYadda : <T>(x: T) => T | Yadda
1111
>x : T
1212

1313
return yaddable;
14-
>yaddable : T | Yadda
14+
>yaddable : Yadda | Awaited<T>
1515
}
1616

1717
interface Yadda {

tests/baselines/reference/forAwaitForUnion.types

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ async function f<T>(source: Iterable<T> | AsyncIterable<T>) {
44
>source : Iterable<T> | AsyncIterable<T>
55

66
for await (const x of source) {
7-
>x : T
7+
>x : Awaited<T>
88
>source : Iterable<T> | AsyncIterable<T>
99
}
1010
}

0 commit comments

Comments
 (0)