Skip to content

Commit

Permalink
Further restrict 'Awaited<T>' auto-wrapping for 'await'
Browse files Browse the repository at this point in the history
  • Loading branch information
rbuckton committed Sep 8, 2021
1 parent 657f0a9 commit 6fd4ca8
Show file tree
Hide file tree
Showing 9 changed files with 917 additions and 24 deletions.
28 changes: 21 additions & 7 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34643,9 +34643,10 @@ namespace ts {
}

/**
* Determines whether a type has a callable `then` member.
* Determines whether a type is an object with a callable `then` member.
*/
function isThenableType(type: Type): boolean {
if (!isTypeAssignableToKind(type, TypeFlags.NonPrimitive)) return false;
const thenFunction = getTypeOfPropertyOfType(type, "then" as __String);
return !!thenFunction && getSignaturesOfType(getTypeWithFacts(thenFunction, TypeFacts.NEUndefinedOrNull), SignatureKind.Call).length > 0;
}
Expand Down Expand Up @@ -34674,6 +34675,14 @@ namespace ts {
}

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
// - `T` is generic, and
// - One of the following applies:
// - `T` has no base constraint, or
// - 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;
}
Expand All @@ -34685,12 +34694,17 @@ namespace ts {

// Only instantiate `Awaited<T>` if `T` contains possibly non-primitive types.
if (isGenericObjectType(type) && !allTypesAssignableToKind(type, TypeFlags.Primitive | TypeFlags.Never)) {
// 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)]);
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)]);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ class C {
>method : () => void

var fn = async () => await this;
>fn : () => Promise<Awaited<this>>
>async () => await this : () => Promise<Awaited<this>>
>await this : Awaited<this>
>fn : () => Promise<this>
>async () => await this : () => Promise<this>
>await this : this
>this : this
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ class C {
>method : () => void

var fn = async () => await this;
>fn : () => Promise<Awaited<this>>
>async () => await this : () => Promise<Awaited<this>>
>await this : Awaited<this>
>fn : () => Promise<this>
>async () => await this : () => Promise<this>
>await this : this
>this : this
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ class C {
>method : () => void

var fn = async () => await this;
>fn : () => Promise<Awaited<this>>
>async () => await this : () => Promise<Awaited<this>>
>await this : Awaited<this>
>fn : () => Promise<this>
>async () => await this : () => Promise<this>
>await this : this
>this : this
}
}
Expand Down
116 changes: 116 additions & 0 deletions tests/baselines/reference/awaitedType.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,122 @@ tests/cases/compiler/awaitedType.ts(22,12): error TS2589: Type instantiation is
])
}

// non-generic
async function f1(x: string) {
// y: string
const y = await x;
}

async function f2(x: unknown) {
// y: unknown
const y = await x;
}

async function f3(x: object) {
// y: object
const y = await x;
}

async function f4(x: Promise<string>) {
// y: string
const y = await x;
}

async function f5(x: Promise<unknown>) {
// y: unknown
const y = await x;
}

async function f6(x: Promise<object>) {
// y: object
const y = await x;
}

// generic

async function f7<T>(x: T) {
// NOTE: T does not belong solely to the domain of primitive types and either does
// not have a base constraint, its base constraint is `any`, `unknown`, `{}`, or `object`,
// or it has a non-primitive base constraint with a callable `then`.

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

async function f8<T extends any>(x: T) {
// NOTE: T does not belong solely to the domain of primitive types and either does
// not have a base constraint, its base constraint is `any`, `unknown`, `{}`, or `object`,
// or it has a non-primitive base constraint with a callable `then`.

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

async function f9<T extends unknown>(x: T) {
// NOTE: T does not belong solely to the domain of primitive types and either does
// not have a base constraint, its base constraint is `any`, `unknown`, `{}`, or `object`,
// or it has a non-primitive base constraint with a callable `then`.

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

async function f10<T extends {}>(x: T) {
// NOTE: T does not belong solely to the domain of primitive types and either does
// not have a base constraint, its base constraint is `any`, `unknown`, `{}`, or `object`,
// or it has a non-primitive base constraint with a callable `then`.

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

async function f11<T extends { then(onfulfilled: (value: unknown) => void): void }>(x: T) {
// NOTE: T does not belong solely to the domain of primitive types and either does
// not have a base constraint, its base constraint is `any`, `unknown`, `{}`, or `object`,
// or it has a non-primitive base constraint with a callable `then`.

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

async function f12<T extends string | object>(x: T) {
// NOTE: T does not belong solely to the domain of primitive types and either does
// not have a base constraint, its base constraint is `any`, `unknown`, `{}`, or `object`,
// or it has a non-primitive base constraint with a callable `then`.

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

async function f13<T extends string>(x: T) {
// NOTE: T belongs to the domain of primitive types

// y: T
const y = await x;
}

async function f14<T extends { x: number }>(x: T) {
// NOTE: T has a non-primitive base constraint without a callable `then`.

// y: T
const y = await x;
}

async function f15<T extends { then: number }>(x: T) {
// NOTE: T has a non-primitive base constraint without a callable `then`.

// y: T
const y = await x;
}

async function f16<T extends number & { then(): void }>(x: T) {
// NOTE: T belongs to the domain of primitive types (regardless of `then`)

// y: T
const y = await x;
}


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

Loading

0 comments on commit 6fd4ca8

Please sign in to comment.