From a56b254ad3c52b598bc5d44f83f3d0a1cf806068 Mon Sep 17 00:00:00 2001 From: Ron Buckton Date: Fri, 21 Oct 2022 08:00:24 -0400 Subject: [PATCH] Include 'this' type parameter in isRelatedTo fast path (#51230) --- src/compiler/checker.ts | 4 ++-- .../baselines/reference/asyncFunctionReturnType.2.js | 11 +++++++++++ .../reference/asyncFunctionReturnType.2.symbols | 10 ++++++++++ .../reference/asyncFunctionReturnType.2.types | 10 ++++++++++ tests/cases/compiler/asyncFunctionReturnType.2.ts | 6 ++++++ 5 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 tests/baselines/reference/asyncFunctionReturnType.2.js create mode 100644 tests/baselines/reference/asyncFunctionReturnType.2.symbols create mode 100644 tests/baselines/reference/asyncFunctionReturnType.2.types create mode 100644 tests/cases/compiler/asyncFunctionReturnType.2.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 5b30941b5ccb3..76b9a21d73f0f 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -36994,7 +36994,7 @@ namespace ts { } // primitives with a `{ then() }` won't be unwrapped/adopted. - if (allTypesAssignableToKind(type, TypeFlags.Primitive | TypeFlags.Never)) { + if (allTypesAssignableToKind(getBaseConstraintOrType(type), TypeFlags.Primitive | TypeFlags.Never)) { return undefined; } @@ -37069,7 +37069,7 @@ namespace ts { * Determines whether a type is an object with a callable `then` member. */ function isThenableType(type: Type): boolean { - if (allTypesAssignableToKind(type, TypeFlags.Primitive | TypeFlags.Never)) { + if (allTypesAssignableToKind(getBaseConstraintOrType(type), TypeFlags.Primitive | TypeFlags.Never)) { // primitive types cannot be considered "thenable" since they are not objects. return false; } diff --git a/tests/baselines/reference/asyncFunctionReturnType.2.js b/tests/baselines/reference/asyncFunctionReturnType.2.js new file mode 100644 index 0000000000000..9405b3f2bb5b6 --- /dev/null +++ b/tests/baselines/reference/asyncFunctionReturnType.2.js @@ -0,0 +1,11 @@ +//// [asyncFunctionReturnType.2.ts] +// https://github.com/microsoft/TypeScript/issues/47291 +class X { + f = async (): Promise => this; +} + +//// [asyncFunctionReturnType.2.js] +// https://github.com/microsoft/TypeScript/issues/47291 +class X { + f = async () => this; +} diff --git a/tests/baselines/reference/asyncFunctionReturnType.2.symbols b/tests/baselines/reference/asyncFunctionReturnType.2.symbols new file mode 100644 index 0000000000000..2e9988be963d3 --- /dev/null +++ b/tests/baselines/reference/asyncFunctionReturnType.2.symbols @@ -0,0 +1,10 @@ +=== tests/cases/compiler/asyncFunctionReturnType.2.ts === +// https://github.com/microsoft/TypeScript/issues/47291 +class X { +>X : Symbol(X, Decl(asyncFunctionReturnType.2.ts, 0, 0)) + + f = async (): Promise => this; +>f : Symbol(X.f, Decl(asyncFunctionReturnType.2.ts, 1, 9)) +>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, --, --)) +>this : Symbol(X, Decl(asyncFunctionReturnType.2.ts, 0, 0)) +} diff --git a/tests/baselines/reference/asyncFunctionReturnType.2.types b/tests/baselines/reference/asyncFunctionReturnType.2.types new file mode 100644 index 0000000000000..1559dda5bf47f --- /dev/null +++ b/tests/baselines/reference/asyncFunctionReturnType.2.types @@ -0,0 +1,10 @@ +=== tests/cases/compiler/asyncFunctionReturnType.2.ts === +// https://github.com/microsoft/TypeScript/issues/47291 +class X { +>X : X + + f = async (): Promise => this; +>f : () => Promise +>async (): Promise => this : () => Promise +>this : this +} diff --git a/tests/cases/compiler/asyncFunctionReturnType.2.ts b/tests/cases/compiler/asyncFunctionReturnType.2.ts new file mode 100644 index 0000000000000..628ecf915ef46 --- /dev/null +++ b/tests/cases/compiler/asyncFunctionReturnType.2.ts @@ -0,0 +1,6 @@ +// @target: esnext + +// https://github.com/microsoft/TypeScript/issues/47291 +class X { + f = async (): Promise => this; +} \ No newline at end of file