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

Fix for Awaited<T> inference #49748

Merged
merged 1 commit into from
Jul 1, 2022
Merged
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
62 changes: 40 additions & 22 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
@@ -36551,6 +36551,37 @@ namespace ts {
type;
}

function isAwaitedTypeNeeded(type: Type) {
// If this is already an `Awaited<T>`, we shouldn't wrap it. This helps to avoid `Awaited<Awaited<T>>` in higher-order.
if (isTypeAny(type) || isAwaitedTypeInstantiation(type)) {
return false;
}

// We only need `Awaited<T>` if `T` contains possibly non-primitive types.
if (isGenericObjectType(type)) {
const baseConstraint = getBaseConstraintOfType(type);
// We only need `Awaited<T>` if `T` has no base constraint, or the base constraint of `T` is `any`, `unknown`, `{}`, `object`,
// or is promise-like.
if (!baseConstraint || (baseConstraint.flags & TypeFlags.AnyOrUnknown) || isEmptyObjectType(baseConstraint) || isThenableType(baseConstraint)) {
return true;
}
}

return false;
}

function tryCreateAwaitedType(type: Type): Type | undefined {
// Nothing to do if `Awaited<T>` doesn't exist
const awaitedSymbol = getGlobalAwaitedSymbol(/*reportErrors*/ true);
if (awaitedSymbol) {
// Unwrap unions that may contain `Awaited<T>`, otherwise its possible to manufacture an `Awaited<Awaited<T> | U>` where
// an `Awaited<T | U>` would suffice.
return getTypeAliasInstantiation(awaitedSymbol, [unwrapAwaitedType(type)]);
}

return undefined;
}

function createAwaitedTypeIfNeeded(type: Type): Type {
// We wrap type `T` in `Awaited<T>` based on the following conditions:
// - `T` is not already an `Awaited<U>`, and
@@ -36560,28 +36591,10 @@ namespace ts {
// - The base constraint of `T` is `any`, `unknown`, `object`, or `{}`, or
// - The base constraint of `T` is an object type with a callable `then` method.

if (isTypeAny(type)) {
return type;
}

// If this is already an `Awaited<T>`, just return it. This helps to avoid `Awaited<Awaited<T>>` in higher-order.
if (isAwaitedTypeInstantiation(type)) {
return type;
}

// Only instantiate `Awaited<T>` if `T` contains possibly non-primitive types.
if (isGenericObjectType(type)) {
const baseConstraint = getBaseConstraintOfType(type);
// Only instantiate `Awaited<T>` if `T` has no base constraint, or the base constraint of `T` is `any`, `unknown`, `{}`, `object`,
// or is promise-like.
if (!baseConstraint || (baseConstraint.flags & TypeFlags.AnyOrUnknown) || isEmptyObjectType(baseConstraint) || isThenableType(baseConstraint)) {
// Nothing to do if `Awaited<T>` doesn't exist
const awaitedSymbol = getGlobalAwaitedSymbol(/*reportErrors*/ true);
if (awaitedSymbol) {
// Unwrap unions that may contain `Awaited<T>`, otherwise its possible to manufacture an `Awaited<Awaited<T> | U>` where
// an `Awaited<T | U>` would suffice.
return getTypeAliasInstantiation(awaitedSymbol, [unwrapAwaitedType(type)]);
}
if (isAwaitedTypeNeeded(type)) {
const awaitedType = tryCreateAwaitedType(type);
if (awaitedType) {
return awaitedType;
}
}

@@ -36643,6 +36656,11 @@ namespace ts {
return typeAsAwaitable.awaitedTypeOfType = mapped;
}

// If `type` is generic and should be wrapped in `Awaited<T>`, return it.
if (isAwaitedTypeNeeded(type)) {
return typeAsAwaitable.awaitedTypeOfType = type;
}

const thisTypeForErrorOut: { value: Type | undefined } = { value: undefined };
const promisedType = getPromisedTypeOfPromise(type, /*errorNode*/ undefined, thisTypeForErrorOut);
if (promisedType) {
11 changes: 10 additions & 1 deletion tests/baselines/reference/awaitedType.errors.txt
Original file line number Diff line number Diff line change
@@ -168,4 +168,13 @@ tests/cases/compiler/awaitedType.ts(22,12): error TS2589: Type instantiation is

// helps with tests where '.types' just prints out the type alias name
type _Expect<TActual extends TExpected, TExpected> = TActual;


// https://github.com/microsoft/TypeScript/issues/48320
async function f17<T extends (...args: any[]) => Promise<any>>(fn: T) {
const ret: Awaited<ReturnType<T>> = await fn(1, 2, 3);
return ret;
}
async function f17_usage() {
const x = await f17(async () => 123 as const);
return { x };
}
20 changes: 19 additions & 1 deletion tests/baselines/reference/awaitedType.js
Original file line number Diff line number Diff line change
@@ -160,7 +160,16 @@ async function f16<T extends number & { then(): void }>(x: T) {

// helps with tests where '.types' just prints out the type alias name
type _Expect<TActual extends TExpected, TExpected> = TActual;


// https://github.com/microsoft/TypeScript/issues/48320
async function f17<T extends (...args: any[]) => Promise<any>>(fn: T) {
const ret: Awaited<ReturnType<T>> = await fn(1, 2, 3);
return ret;
}
async function f17_usage() {
const x = await f17(async () => 123 as const);
return { x };
}

//// [awaitedType.js]
async function main() {
@@ -263,3 +272,12 @@ async function f16(x) {
// y: T
const y = await x;
}
// https://github.com/microsoft/TypeScript/issues/48320
async function f17(fn) {
const ret = await fn(1, 2, 3);
return ret;
}
async function f17_usage() {
const x = await f17(async () => 123);
return { x };
}
30 changes: 30 additions & 0 deletions tests/baselines/reference/awaitedType.symbols
Original file line number Diff line number Diff line change
@@ -403,3 +403,33 @@ type _Expect<TActual extends TExpected, TExpected> = TActual;
>TExpected : Symbol(TExpected, Decl(awaitedType.ts, 160, 39))
>TActual : Symbol(TActual, Decl(awaitedType.ts, 160, 13))

// https://github.com/microsoft/TypeScript/issues/48320
async function f17<T extends (...args: any[]) => Promise<any>>(fn: T) {
>f17 : Symbol(f17, Decl(awaitedType.ts, 160, 61))
>T : Symbol(T, Decl(awaitedType.ts, 163, 19))
>args : Symbol(args, Decl(awaitedType.ts, 163, 30))
>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2018.promise.d.ts, --, --))
>fn : Symbol(fn, Decl(awaitedType.ts, 163, 63))
>T : Symbol(T, Decl(awaitedType.ts, 163, 19))

const ret: Awaited<ReturnType<T>> = await fn(1, 2, 3);
>ret : Symbol(ret, Decl(awaitedType.ts, 164, 9))
>Awaited : Symbol(Awaited, Decl(lib.es5.d.ts, --, --))
>ReturnType : Symbol(ReturnType, Decl(lib.es5.d.ts, --, --))
>T : Symbol(T, Decl(awaitedType.ts, 163, 19))
>fn : Symbol(fn, Decl(awaitedType.ts, 163, 63))

return ret;
>ret : Symbol(ret, Decl(awaitedType.ts, 164, 9))
}
async function f17_usage() {
>f17_usage : Symbol(f17_usage, Decl(awaitedType.ts, 166, 1))

const x = await f17(async () => 123 as const);
>x : Symbol(x, Decl(awaitedType.ts, 168, 9))
>f17 : Symbol(f17, Decl(awaitedType.ts, 160, 61))
>const : Symbol(const)

return { x };
>x : Symbol(x, Decl(awaitedType.ts, 169, 12))
}
38 changes: 36 additions & 2 deletions tests/baselines/reference/awaitedType.types
Original file line number Diff line number Diff line change
@@ -278,8 +278,8 @@ async function f11<T extends { then(onfulfilled: (value: unknown) => void): void

// y: Awaited<T>
const y = await x;
>y : unknown
>await x : unknown
>y : Awaited<T>
>await x : Awaited<T>
>x : T
}

@@ -358,3 +358,37 @@ async function f16<T extends number & { then(): void }>(x: T) {
type _Expect<TActual extends TExpected, TExpected> = TActual;
>_Expect : TActual

// https://github.com/microsoft/TypeScript/issues/48320
async function f17<T extends (...args: any[]) => Promise<any>>(fn: T) {
>f17 : <T extends (...args: any[]) => Promise<any>>(fn: T) => Promise<ReturnType<T>>
>args : any[]
>fn : T

const ret: Awaited<ReturnType<T>> = await fn(1, 2, 3);
>ret : Awaited<ReturnType<T>>
>await fn(1, 2, 3) : any
>fn(1, 2, 3) : Promise<any>
>fn : T
>1 : 1
>2 : 2
>3 : 3

return ret;
>ret : Awaited<ReturnType<T>>
}
async function f17_usage() {
>f17_usage : () => Promise<{ x: 123; }>

const x = await f17(async () => 123 as const);
>x : 123
>await f17(async () => 123 as const) : 123
>f17(async () => 123 as const) : Promise<Promise<123>>
>f17 : <T extends (...args: any[]) => Promise<any>>(fn: T) => Promise<ReturnType<T>>
>async () => 123 as const : () => Promise<123>
>123 as const : 123
>123 : 123

return { x };
>{ x } : { x: 123; }
>x : 123
}
10 changes: 10 additions & 0 deletions tests/cases/compiler/awaitedType.ts
Original file line number Diff line number Diff line change
@@ -162,3 +162,13 @@ async function f16<T extends number & { then(): void }>(x: T) {

// helps with tests where '.types' just prints out the type alias name
type _Expect<TActual extends TExpected, TExpected> = TActual;

// https://github.com/microsoft/TypeScript/issues/48320
async function f17<T extends (...args: any[]) => Promise<any>>(fn: T) {
const ret: Awaited<ReturnType<T>> = await fn(1, 2, 3);
return ret;
}
async function f17_usage() {
const x = await f17(async () => 123 as const);
return { x };
}