From 8f6ad87d9980d028b312924c9bca1ff92ddb02a8 Mon Sep 17 00:00:00 2001 From: Zzzen Date: Tue, 13 Jul 2021 19:01:12 +0800 Subject: [PATCH] support contextual return type of iife --- src/compiler/checker.ts | 4 ++ .../reference/contextualReturnTypeOfIIFE.js | 61 +++++++++++++++++++ .../contextualReturnTypeOfIIFE.symbols | 28 +++++++++ .../contextualReturnTypeOfIIFE.types | 46 ++++++++++++++ .../reference/generatorTypeCheck27.symbols | 2 + .../reference/generatorTypeCheck27.types | 16 ++--- .../reference/generatorTypeCheck29.symbols | 2 + .../reference/generatorTypeCheck29.types | 16 ++--- .../reference/generatorTypeCheck30.symbols | 2 + .../reference/generatorTypeCheck30.types | 16 ++--- .../reference/generatorTypeCheck31.errors.txt | 8 +-- .../reference/generatorTypeCheck31.types | 4 +- .../types.asyncGenerators.es2018.1.types | 54 ++++++++-------- .../types.asyncGenerators.es2018.2.errors.txt | 32 ++++------ .../types.asyncGenerators.es2018.2.types | 54 ++++++++-------- .../compiler/contextualReturnTypeOfIIFE.ts | 13 ++++ 16 files changed, 254 insertions(+), 104 deletions(-) create mode 100644 tests/baselines/reference/contextualReturnTypeOfIIFE.js create mode 100644 tests/baselines/reference/contextualReturnTypeOfIIFE.symbols create mode 100644 tests/baselines/reference/contextualReturnTypeOfIIFE.types create mode 100644 tests/cases/compiler/contextualReturnTypeOfIIFE.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index f5f458a560573..bfe68f7c703a7 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -25309,6 +25309,10 @@ namespace ts { if (signature && !isResolvingReturnTypeOfSignature(signature)) { return getReturnTypeOfSignature(signature); } + const iife = getImmediatelyInvokedFunctionExpression(functionDecl); + if (iife) { + return getContextualType(iife); + } return undefined; } diff --git a/tests/baselines/reference/contextualReturnTypeOfIIFE.js b/tests/baselines/reference/contextualReturnTypeOfIIFE.js new file mode 100644 index 0000000000000..a742ee232fb3a --- /dev/null +++ b/tests/baselines/reference/contextualReturnTypeOfIIFE.js @@ -0,0 +1,61 @@ +//// [contextualReturnTypeOfIIFE.ts] +const test1: Promise<[one: number, two: string]> = (async () => { + return [1, 'two']; +})(); + +const test2: Promise<[one: number, two: string]> = new Promise( + (resolve) => resolve([1, 'two']), +); + +const obj: { foo: [one: number, two: string] } = { + foo: (() => [1, 'two'])() +}; + + +//// [contextualReturnTypeOfIIFE.js] +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +var __generator = (this && this.__generator) || function (thisArg, body) { + var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; + return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; + function verb(n) { return function (v) { return step([n, v]); }; } + function step(op) { + if (f) throw new TypeError("Generator is already executing."); + while (_) try { + if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; + if (y = 0, t) op = [op[0] & 2, t.value]; + switch (op[0]) { + case 0: case 1: t = op; break; + case 4: _.label++; return { value: op[1], done: false }; + case 5: _.label++; y = op[1]; op = [0]; continue; + case 7: op = _.ops.pop(); _.trys.pop(); continue; + default: + if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } + if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } + if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } + if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } + if (t[2]) _.ops.pop(); + _.trys.pop(); continue; + } + op = body.call(thisArg, _); + } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } + if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; + } +}; +var _this = this; +var test1 = (function () { return __awaiter(_this, void 0, void 0, function () { + return __generator(this, function (_a) { + return [2 /*return*/, [1, 'two']]; + }); +}); })(); +var test2 = new Promise(function (resolve) { return resolve([1, 'two']); }); +var obj = { + foo: (function () { return [1, 'two']; })() +}; diff --git a/tests/baselines/reference/contextualReturnTypeOfIIFE.symbols b/tests/baselines/reference/contextualReturnTypeOfIIFE.symbols new file mode 100644 index 0000000000000..2602cae384c0a --- /dev/null +++ b/tests/baselines/reference/contextualReturnTypeOfIIFE.symbols @@ -0,0 +1,28 @@ +=== tests/cases/compiler/contextualReturnTypeOfIIFE.ts === +const test1: Promise<[one: number, two: string]> = (async () => { +>test1 : Symbol(test1, Decl(contextualReturnTypeOfIIFE.ts, 0, 5)) +>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, --, --)) + + return [1, 'two']; +})(); + +const test2: Promise<[one: number, two: string]> = new Promise( +>test2 : Symbol(test2, Decl(contextualReturnTypeOfIIFE.ts, 4, 5)) +>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, --, --)) +>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, --, --)) + + (resolve) => resolve([1, 'two']), +>resolve : Symbol(resolve, Decl(contextualReturnTypeOfIIFE.ts, 5, 5)) +>resolve : Symbol(resolve, Decl(contextualReturnTypeOfIIFE.ts, 5, 5)) + +); + +const obj: { foo: [one: number, two: string] } = { +>obj : Symbol(obj, Decl(contextualReturnTypeOfIIFE.ts, 8, 5)) +>foo : Symbol(foo, Decl(contextualReturnTypeOfIIFE.ts, 8, 12)) + + foo: (() => [1, 'two'])() +>foo : Symbol(foo, Decl(contextualReturnTypeOfIIFE.ts, 8, 50)) + +}; + diff --git a/tests/baselines/reference/contextualReturnTypeOfIIFE.types b/tests/baselines/reference/contextualReturnTypeOfIIFE.types new file mode 100644 index 0000000000000..b5289ca2c7640 --- /dev/null +++ b/tests/baselines/reference/contextualReturnTypeOfIIFE.types @@ -0,0 +1,46 @@ +=== tests/cases/compiler/contextualReturnTypeOfIIFE.ts === +const test1: Promise<[one: number, two: string]> = (async () => { +>test1 : Promise<[one: number, two: string]> +>(async () => { return [1, 'two'];})() : Promise<[number, string]> +>(async () => { return [1, 'two'];}) : () => Promise<[number, string]> +>async () => { return [1, 'two'];} : () => Promise<[number, string]> + + return [1, 'two']; +>[1, 'two'] : [number, string] +>1 : 1 +>'two' : "two" + +})(); + +const test2: Promise<[one: number, two: string]> = new Promise( +>test2 : Promise<[one: number, two: string]> +>new Promise( (resolve) => resolve([1, 'two']),) : Promise<[one: number, two: string]> +>Promise : PromiseConstructor + + (resolve) => resolve([1, 'two']), +>(resolve) => resolve([1, 'two']) : (resolve: (value: [one: number, two: string] | PromiseLike<[one: number, two: string]>) => void) => void +>resolve : (value: [one: number, two: string] | PromiseLike<[one: number, two: string]>) => void +>resolve([1, 'two']) : void +>resolve : (value: [one: number, two: string] | PromiseLike<[one: number, two: string]>) => void +>[1, 'two'] : [number, string] +>1 : 1 +>'two' : "two" + +); + +const obj: { foo: [one: number, two: string] } = { +>obj : { foo: [one: number, two: string]; } +>foo : [one: number, two: string] +>{ foo: (() => [1, 'two'])()} : { foo: [number, string]; } + + foo: (() => [1, 'two'])() +>foo : [number, string] +>(() => [1, 'two'])() : [number, string] +>(() => [1, 'two']) : () => [number, string] +>() => [1, 'two'] : () => [number, string] +>[1, 'two'] : [number, string] +>1 : 1 +>'two' : "two" + +}; + diff --git a/tests/baselines/reference/generatorTypeCheck27.symbols b/tests/baselines/reference/generatorTypeCheck27.symbols index 58f5e56242385..1feb7aed7e4f0 100644 --- a/tests/baselines/reference/generatorTypeCheck27.symbols +++ b/tests/baselines/reference/generatorTypeCheck27.symbols @@ -7,7 +7,9 @@ function* g(): IterableIterator<(x: string) => number> { yield * function* () { yield x => x.length; >x : Symbol(x, Decl(generatorTypeCheck27.ts, 2, 13)) +>x.length : Symbol(String.length, Decl(lib.es5.d.ts, --, --)) >x : Symbol(x, Decl(generatorTypeCheck27.ts, 2, 13)) +>length : Symbol(String.length, Decl(lib.es5.d.ts, --, --)) } (); } diff --git a/tests/baselines/reference/generatorTypeCheck27.types b/tests/baselines/reference/generatorTypeCheck27.types index 58306e1fb4aec..35bf7edc864ff 100644 --- a/tests/baselines/reference/generatorTypeCheck27.types +++ b/tests/baselines/reference/generatorTypeCheck27.types @@ -5,16 +5,16 @@ function* g(): IterableIterator<(x: string) => number> { yield * function* () { >yield * function* () { yield x => x.length; } () : void ->function* () { yield x => x.length; } () : Generator<(x: any) => any, void, unknown> ->function* () { yield x => x.length; } : () => Generator<(x: any) => any, void, unknown> +>function* () { yield x => x.length; } () : Generator<(x: string) => number, void, undefined> +>function* () { yield x => x.length; } : () => Generator<(x: string) => number, void, undefined> yield x => x.length; ->yield x => x.length : any ->x => x.length : (x: any) => any ->x : any ->x.length : any ->x : any ->length : any +>yield x => x.length : undefined +>x => x.length : (x: string) => number +>x : string +>x.length : number +>x : string +>length : number } (); } diff --git a/tests/baselines/reference/generatorTypeCheck29.symbols b/tests/baselines/reference/generatorTypeCheck29.symbols index 5ea31ccb0cd67..e4c1b8850c557 100644 --- a/tests/baselines/reference/generatorTypeCheck29.symbols +++ b/tests/baselines/reference/generatorTypeCheck29.symbols @@ -8,7 +8,9 @@ function* g2(): Iterator number>> { yield function* () { yield x => x.length; >x : Symbol(x, Decl(generatorTypeCheck29.ts, 2, 13)) +>x.length : Symbol(String.length, Decl(lib.es5.d.ts, --, --)) >x : Symbol(x, Decl(generatorTypeCheck29.ts, 2, 13)) +>length : Symbol(String.length, Decl(lib.es5.d.ts, --, --)) } () } diff --git a/tests/baselines/reference/generatorTypeCheck29.types b/tests/baselines/reference/generatorTypeCheck29.types index 0ce49791a22c6..71d781eaa4d07 100644 --- a/tests/baselines/reference/generatorTypeCheck29.types +++ b/tests/baselines/reference/generatorTypeCheck29.types @@ -5,16 +5,16 @@ function* g2(): Iterator number>> { yield function* () { >yield function* () { yield x => x.length; } () : undefined ->function* () { yield x => x.length; } () : Generator<(x: any) => any, void, unknown> ->function* () { yield x => x.length; } : () => Generator<(x: any) => any, void, unknown> +>function* () { yield x => x.length; } () : Generator<(x: string) => number, void, undefined> +>function* () { yield x => x.length; } : () => Generator<(x: string) => number, void, undefined> yield x => x.length; ->yield x => x.length : any ->x => x.length : (x: any) => any ->x : any ->x.length : any ->x : any ->length : any +>yield x => x.length : undefined +>x => x.length : (x: string) => number +>x : string +>x.length : number +>x : string +>length : number } () } diff --git a/tests/baselines/reference/generatorTypeCheck30.symbols b/tests/baselines/reference/generatorTypeCheck30.symbols index f1626cf067f86..d7d346db0adb3 100644 --- a/tests/baselines/reference/generatorTypeCheck30.symbols +++ b/tests/baselines/reference/generatorTypeCheck30.symbols @@ -8,7 +8,9 @@ function* g2(): Iterator number>> { yield function* () { yield x => x.length; >x : Symbol(x, Decl(generatorTypeCheck30.ts, 2, 13)) +>x.length : Symbol(String.length, Decl(lib.es5.d.ts, --, --)) >x : Symbol(x, Decl(generatorTypeCheck30.ts, 2, 13)) +>length : Symbol(String.length, Decl(lib.es5.d.ts, --, --)) } () } diff --git a/tests/baselines/reference/generatorTypeCheck30.types b/tests/baselines/reference/generatorTypeCheck30.types index 094ed3bf33758..12f06dc09dfb2 100644 --- a/tests/baselines/reference/generatorTypeCheck30.types +++ b/tests/baselines/reference/generatorTypeCheck30.types @@ -5,16 +5,16 @@ function* g2(): Iterator number>> { yield function* () { >yield function* () { yield x => x.length; } () : undefined ->function* () { yield x => x.length; } () : Generator<(x: any) => any, void, unknown> ->function* () { yield x => x.length; } : () => Generator<(x: any) => any, void, unknown> +>function* () { yield x => x.length; } () : Generator<(x: string) => number, void, undefined> +>function* () { yield x => x.length; } : () => Generator<(x: string) => number, void, undefined> yield x => x.length; ->yield x => x.length : any ->x => x.length : (x: any) => any ->x : any ->x.length : any ->x : any ->length : any +>yield x => x.length : undefined +>x => x.length : (x: string) => number +>x : string +>x.length : number +>x : string +>length : number } () } diff --git a/tests/baselines/reference/generatorTypeCheck31.errors.txt b/tests/baselines/reference/generatorTypeCheck31.errors.txt index 0ed5dcbe0e08f..9aaf3b745ee4e 100644 --- a/tests/baselines/reference/generatorTypeCheck31.errors.txt +++ b/tests/baselines/reference/generatorTypeCheck31.errors.txt @@ -1,5 +1,5 @@ -tests/cases/conformance/es6/yieldExpressions/generatorTypeCheck31.ts(2,11): error TS2322: Type 'Generator<(x: any) => any, void, unknown>' is not assignable to type '() => Iterable<(x: string) => number>'. - Type 'Generator<(x: any) => any, void, unknown>' provides no match for the signature '(): Iterable<(x: string) => number>'. +tests/cases/conformance/es6/yieldExpressions/generatorTypeCheck31.ts(2,11): error TS2322: Type 'Generator<(x: any) => any, void, any>' is not assignable to type '() => Iterable<(x: string) => number>'. + Type 'Generator<(x: any) => any, void, any>' provides no match for the signature '(): Iterable<(x: string) => number>'. ==== tests/cases/conformance/es6/yieldExpressions/generatorTypeCheck31.ts (1 errors) ==== @@ -10,6 +10,6 @@ tests/cases/conformance/es6/yieldExpressions/generatorTypeCheck31.ts(2,11): erro ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ } () ~~~~~~~~ -!!! error TS2322: Type 'Generator<(x: any) => any, void, unknown>' is not assignable to type '() => Iterable<(x: string) => number>'. -!!! error TS2322: Type 'Generator<(x: any) => any, void, unknown>' provides no match for the signature '(): Iterable<(x: string) => number>'. +!!! error TS2322: Type 'Generator<(x: any) => any, void, any>' is not assignable to type '() => Iterable<(x: string) => number>'. +!!! error TS2322: Type 'Generator<(x: any) => any, void, any>' provides no match for the signature '(): Iterable<(x: string) => number>'. } \ No newline at end of file diff --git a/tests/baselines/reference/generatorTypeCheck31.types b/tests/baselines/reference/generatorTypeCheck31.types index c15eab96fe9e0..2a3828322fffb 100644 --- a/tests/baselines/reference/generatorTypeCheck31.types +++ b/tests/baselines/reference/generatorTypeCheck31.types @@ -5,8 +5,8 @@ function* g2(): Iterator<() => Iterable<(x: string) => number>> { yield function* () { >yield function* () { yield x => x.length; } () : undefined ->function* () { yield x => x.length; } () : Generator<(x: any) => any, void, unknown> ->function* () { yield x => x.length; } : () => Generator<(x: any) => any, void, unknown> +>function* () { yield x => x.length; } () : Generator<(x: any) => any, void, any> +>function* () { yield x => x.length; } : () => Generator<(x: any) => any, void, any> yield x => x.length; >yield x => x.length : any diff --git a/tests/baselines/reference/types.asyncGenerators.es2018.1.types b/tests/baselines/reference/types.asyncGenerators.es2018.1.types index e0c769a74d67b..10f961da0a2bf 100644 --- a/tests/baselines/reference/types.asyncGenerators.es2018.1.types +++ b/tests/baselines/reference/types.asyncGenerators.es2018.1.types @@ -122,14 +122,14 @@ const assignability4: () => AsyncIterableIterator = async function * () }; const assignability5: () => AsyncIterableIterator = async function * () { >assignability5 : () => AsyncIterableIterator ->async function * () { yield* (async function * () { yield 1; })();} : () => AsyncGenerator +>async function * () { yield* (async function * () { yield 1; })();} : () => AsyncGenerator yield* (async function * () { yield 1; })(); >yield* (async function * () { yield 1; })() : void ->(async function * () { yield 1; })() : AsyncGenerator ->(async function * () { yield 1; }) : () => AsyncGenerator ->async function * () { yield 1; } : () => AsyncGenerator ->yield 1 : any +>(async function * () { yield 1; })() : AsyncGenerator +>(async function * () { yield 1; }) : () => AsyncGenerator +>async function * () { yield 1; } : () => AsyncGenerator +>yield 1 : undefined >1 : 1 }; @@ -182,14 +182,14 @@ const assignability9: () => AsyncIterable = async function * () { }; const assignability10: () => AsyncIterable = async function * () { >assignability10 : () => AsyncIterable ->async function * () { yield* (async function * () { yield 1; })();} : () => AsyncGenerator +>async function * () { yield* (async function * () { yield 1; })();} : () => AsyncGenerator yield* (async function * () { yield 1; })(); >yield* (async function * () { yield 1; })() : void ->(async function * () { yield 1; })() : AsyncGenerator ->(async function * () { yield 1; }) : () => AsyncGenerator ->async function * () { yield 1; } : () => AsyncGenerator ->yield 1 : any +>(async function * () { yield 1; })() : AsyncGenerator +>(async function * () { yield 1; }) : () => AsyncGenerator +>async function * () { yield 1; } : () => AsyncGenerator +>yield 1 : undefined >1 : 1 }; @@ -242,14 +242,14 @@ const assignability14: () => AsyncIterator = async function * () { }; const assignability15: () => AsyncIterator = async function * () { >assignability15 : () => AsyncIterator ->async function * () { yield* (async function * () { yield 1; })();} : () => AsyncGenerator +>async function * () { yield* (async function * () { yield 1; })();} : () => AsyncGenerator yield* (async function * () { yield 1; })(); >yield* (async function * () { yield 1; })() : void ->(async function * () { yield 1; })() : AsyncGenerator ->(async function * () { yield 1; }) : () => AsyncGenerator ->async function * () { yield 1; } : () => AsyncGenerator ->yield 1 : any +>(async function * () { yield 1; })() : AsyncGenerator +>(async function * () { yield 1; }) : () => AsyncGenerator +>async function * () { yield 1; } : () => AsyncGenerator +>yield 1 : undefined >1 : 1 }; @@ -297,10 +297,10 @@ async function * explicitReturnType5(): AsyncIterableIterator { yield* (async function * () { yield 1; })(); >yield* (async function * () { yield 1; })() : void ->(async function * () { yield 1; })() : AsyncGenerator ->(async function * () { yield 1; }) : () => AsyncGenerator ->async function * () { yield 1; } : () => AsyncGenerator ->yield 1 : any +>(async function * () { yield 1; })() : AsyncGenerator +>(async function * () { yield 1; }) : () => AsyncGenerator +>async function * () { yield 1; } : () => AsyncGenerator +>yield 1 : undefined >1 : 1 } async function * explicitReturnType6(): AsyncIterable { @@ -347,10 +347,10 @@ async function * explicitReturnType10(): AsyncIterable { yield* (async function * () { yield 1; })(); >yield* (async function * () { yield 1; })() : void ->(async function * () { yield 1; })() : AsyncGenerator ->(async function * () { yield 1; }) : () => AsyncGenerator ->async function * () { yield 1; } : () => AsyncGenerator ->yield 1 : any +>(async function * () { yield 1; })() : AsyncGenerator +>(async function * () { yield 1; }) : () => AsyncGenerator +>async function * () { yield 1; } : () => AsyncGenerator +>yield 1 : undefined >1 : 1 } async function * explicitReturnType11(): AsyncIterator { @@ -397,10 +397,10 @@ async function * explicitReturnType15(): AsyncIterator { yield* (async function * () { yield 1; })(); >yield* (async function * () { yield 1; })() : void ->(async function * () { yield 1; })() : AsyncGenerator ->(async function * () { yield 1; }) : () => AsyncGenerator ->async function * () { yield 1; } : () => AsyncGenerator ->yield 1 : any +>(async function * () { yield 1; })() : AsyncGenerator +>(async function * () { yield 1; }) : () => AsyncGenerator +>async function * () { yield 1; } : () => AsyncGenerator +>yield 1 : undefined >1 : 1 } async function * explicitReturnType16(): {} { diff --git a/tests/baselines/reference/types.asyncGenerators.es2018.2.errors.txt b/tests/baselines/reference/types.asyncGenerators.es2018.2.errors.txt index 2b96990092f3d..b0f976b703691 100644 --- a/tests/baselines/reference/types.asyncGenerators.es2018.2.errors.txt +++ b/tests/baselines/reference/types.asyncGenerators.es2018.2.errors.txt @@ -10,26 +10,22 @@ tests/cases/conformance/types/asyncGenerators/types.asyncGenerators.es2018.2.ts( Type 'string' is not assignable to type 'number'. tests/cases/conformance/types/asyncGenerators/types.asyncGenerators.es2018.2.ts(13,7): error TS2322: Type '() => AsyncGenerator' is not assignable to type '() => AsyncIterableIterator'. Type 'AsyncGenerator' is not assignable to type 'AsyncIterableIterator'. -tests/cases/conformance/types/asyncGenerators/types.asyncGenerators.es2018.2.ts(16,7): error TS2322: Type '() => AsyncGenerator' is not assignable to type '() => AsyncIterableIterator'. - Call signature return types 'AsyncGenerator' and 'AsyncIterableIterator' are incompatible. - The types returned by 'next(...)' are incompatible between these types. - Type 'Promise>' is not assignable to type 'Promise>'. +tests/cases/conformance/types/asyncGenerators/types.asyncGenerators.es2018.2.ts(16,7): error TS2322: Type '() => AsyncGenerator' is not assignable to type '() => AsyncIterableIterator'. + Type 'AsyncGenerator' is not assignable to type 'AsyncIterableIterator'. tests/cases/conformance/types/asyncGenerators/types.asyncGenerators.es2018.2.ts(19,7): error TS2322: Type '() => AsyncGenerator' is not assignable to type '() => AsyncIterable'. Call signature return types 'AsyncGenerator' and 'AsyncIterable' are incompatible. The types of '[Symbol.asyncIterator]().next' are incompatible between these types. Type '(...args: [] | [undefined]) => Promise>' is not assignable to type '(...args: [] | [undefined]) => Promise>'. tests/cases/conformance/types/asyncGenerators/types.asyncGenerators.es2018.2.ts(22,7): error TS2322: Type '() => AsyncGenerator' is not assignable to type '() => AsyncIterable'. Type 'AsyncGenerator' is not assignable to type 'AsyncIterable'. -tests/cases/conformance/types/asyncGenerators/types.asyncGenerators.es2018.2.ts(25,7): error TS2322: Type '() => AsyncGenerator' is not assignable to type '() => AsyncIterable'. - Call signature return types 'AsyncGenerator' and 'AsyncIterable' are incompatible. - The types of '[Symbol.asyncIterator]().next' are incompatible between these types. - Type '(...args: [] | [unknown]) => Promise>' is not assignable to type '(...args: [] | [undefined]) => Promise>'. +tests/cases/conformance/types/asyncGenerators/types.asyncGenerators.es2018.2.ts(25,7): error TS2322: Type '() => AsyncGenerator' is not assignable to type '() => AsyncIterable'. + Type 'AsyncGenerator' is not assignable to type 'AsyncIterable'. tests/cases/conformance/types/asyncGenerators/types.asyncGenerators.es2018.2.ts(28,7): error TS2322: Type '() => AsyncGenerator' is not assignable to type '() => AsyncIterator'. Type 'AsyncGenerator' is not assignable to type 'AsyncIterator'. tests/cases/conformance/types/asyncGenerators/types.asyncGenerators.es2018.2.ts(31,7): error TS2322: Type '() => AsyncGenerator' is not assignable to type '() => AsyncIterator'. Type 'AsyncGenerator' is not assignable to type 'AsyncIterator'. -tests/cases/conformance/types/asyncGenerators/types.asyncGenerators.es2018.2.ts(34,7): error TS2322: Type '() => AsyncGenerator' is not assignable to type '() => AsyncIterator'. - Type 'AsyncGenerator' is not assignable to type 'AsyncIterator'. +tests/cases/conformance/types/asyncGenerators/types.asyncGenerators.es2018.2.ts(34,7): error TS2322: Type '() => AsyncGenerator' is not assignable to type '() => AsyncIterator'. + Type 'AsyncGenerator' is not assignable to type 'AsyncIterator'. tests/cases/conformance/types/asyncGenerators/types.asyncGenerators.es2018.2.ts(38,11): error TS2322: Type 'string' is not assignable to type 'number'. tests/cases/conformance/types/asyncGenerators/types.asyncGenerators.es2018.2.ts(41,12): error TS2322: Type 'string' is not assignable to type 'number'. tests/cases/conformance/types/asyncGenerators/types.asyncGenerators.es2018.2.ts(44,12): error TS2322: Type 'string' is not assignable to type 'number'. @@ -83,10 +79,8 @@ tests/cases/conformance/types/asyncGenerators/types.asyncGenerators.es2018.2.ts( }; const assignability3: () => AsyncIterableIterator = async function * () { ~~~~~~~~~~~~~~ -!!! error TS2322: Type '() => AsyncGenerator' is not assignable to type '() => AsyncIterableIterator'. -!!! error TS2322: Call signature return types 'AsyncGenerator' and 'AsyncIterableIterator' are incompatible. -!!! error TS2322: The types returned by 'next(...)' are incompatible between these types. -!!! error TS2322: Type 'Promise>' is not assignable to type 'Promise>'. +!!! error TS2322: Type '() => AsyncGenerator' is not assignable to type '() => AsyncIterableIterator'. +!!! error TS2322: Type 'AsyncGenerator' is not assignable to type 'AsyncIterableIterator'. yield* (async function * () { yield "a"; })(); }; const assignability4: () => AsyncIterable = async function * () { @@ -105,10 +99,8 @@ tests/cases/conformance/types/asyncGenerators/types.asyncGenerators.es2018.2.ts( }; const assignability6: () => AsyncIterable = async function * () { ~~~~~~~~~~~~~~ -!!! error TS2322: Type '() => AsyncGenerator' is not assignable to type '() => AsyncIterable'. -!!! error TS2322: Call signature return types 'AsyncGenerator' and 'AsyncIterable' are incompatible. -!!! error TS2322: The types of '[Symbol.asyncIterator]().next' are incompatible between these types. -!!! error TS2322: Type '(...args: [] | [unknown]) => Promise>' is not assignable to type '(...args: [] | [undefined]) => Promise>'. +!!! error TS2322: Type '() => AsyncGenerator' is not assignable to type '() => AsyncIterable'. +!!! error TS2322: Type 'AsyncGenerator' is not assignable to type 'AsyncIterable'. yield* (async function * () { yield "a"; })(); }; const assignability7: () => AsyncIterator = async function * () { @@ -125,8 +117,8 @@ tests/cases/conformance/types/asyncGenerators/types.asyncGenerators.es2018.2.ts( }; const assignability9: () => AsyncIterator = async function * () { ~~~~~~~~~~~~~~ -!!! error TS2322: Type '() => AsyncGenerator' is not assignable to type '() => AsyncIterator'. -!!! error TS2322: Type 'AsyncGenerator' is not assignable to type 'AsyncIterator'. +!!! error TS2322: Type '() => AsyncGenerator' is not assignable to type '() => AsyncIterator'. +!!! error TS2322: Type 'AsyncGenerator' is not assignable to type 'AsyncIterator'. yield* (async function * () { yield "a"; })(); }; async function * explicitReturnType1(): AsyncIterableIterator { diff --git a/tests/baselines/reference/types.asyncGenerators.es2018.2.types b/tests/baselines/reference/types.asyncGenerators.es2018.2.types index 4d52f869d1e8a..15e7936ba6351 100644 --- a/tests/baselines/reference/types.asyncGenerators.es2018.2.types +++ b/tests/baselines/reference/types.asyncGenerators.es2018.2.types @@ -49,14 +49,14 @@ const assignability2: () => AsyncIterableIterator = async function * () }; const assignability3: () => AsyncIterableIterator = async function * () { >assignability3 : () => AsyncIterableIterator ->async function * () { yield* (async function * () { yield "a"; })();} : () => AsyncGenerator +>async function * () { yield* (async function * () { yield "a"; })();} : () => AsyncGenerator yield* (async function * () { yield "a"; })(); >yield* (async function * () { yield "a"; })() : void ->(async function * () { yield "a"; })() : AsyncGenerator ->(async function * () { yield "a"; }) : () => AsyncGenerator ->async function * () { yield "a"; } : () => AsyncGenerator ->yield "a" : any +>(async function * () { yield "a"; })() : AsyncGenerator +>(async function * () { yield "a"; }) : () => AsyncGenerator +>async function * () { yield "a"; } : () => AsyncGenerator +>yield "a" : undefined >"a" : "a" }; @@ -82,14 +82,14 @@ const assignability5: () => AsyncIterable = async function * () { }; const assignability6: () => AsyncIterable = async function * () { >assignability6 : () => AsyncIterable ->async function * () { yield* (async function * () { yield "a"; })();} : () => AsyncGenerator +>async function * () { yield* (async function * () { yield "a"; })();} : () => AsyncGenerator yield* (async function * () { yield "a"; })(); >yield* (async function * () { yield "a"; })() : void ->(async function * () { yield "a"; })() : AsyncGenerator ->(async function * () { yield "a"; }) : () => AsyncGenerator ->async function * () { yield "a"; } : () => AsyncGenerator ->yield "a" : any +>(async function * () { yield "a"; })() : AsyncGenerator +>(async function * () { yield "a"; }) : () => AsyncGenerator +>async function * () { yield "a"; } : () => AsyncGenerator +>yield "a" : undefined >"a" : "a" }; @@ -115,14 +115,14 @@ const assignability8: () => AsyncIterator = async function * () { }; const assignability9: () => AsyncIterator = async function * () { >assignability9 : () => AsyncIterator ->async function * () { yield* (async function * () { yield "a"; })();} : () => AsyncGenerator +>async function * () { yield* (async function * () { yield "a"; })();} : () => AsyncGenerator yield* (async function * () { yield "a"; })(); >yield* (async function * () { yield "a"; })() : void ->(async function * () { yield "a"; })() : AsyncGenerator ->(async function * () { yield "a"; }) : () => AsyncGenerator ->async function * () { yield "a"; } : () => AsyncGenerator ->yield "a" : any +>(async function * () { yield "a"; })() : AsyncGenerator +>(async function * () { yield "a"; }) : () => AsyncGenerator +>async function * () { yield "a"; } : () => AsyncGenerator +>yield "a" : undefined >"a" : "a" }; @@ -147,10 +147,10 @@ async function * explicitReturnType3(): AsyncIterableIterator { yield* (async function * () { yield "a"; })(); >yield* (async function * () { yield "a"; })() : void ->(async function * () { yield "a"; })() : AsyncGenerator ->(async function * () { yield "a"; }) : () => AsyncGenerator ->async function * () { yield "a"; } : () => AsyncGenerator ->yield "a" : any +>(async function * () { yield "a"; })() : AsyncGenerator +>(async function * () { yield "a"; }) : () => AsyncGenerator +>async function * () { yield "a"; } : () => AsyncGenerator +>yield "a" : undefined >"a" : "a" } async function * explicitReturnType4(): AsyncIterable { @@ -174,10 +174,10 @@ async function * explicitReturnType6(): AsyncIterable { yield* (async function * () { yield "a"; })(); >yield* (async function * () { yield "a"; })() : void ->(async function * () { yield "a"; })() : AsyncGenerator ->(async function * () { yield "a"; }) : () => AsyncGenerator ->async function * () { yield "a"; } : () => AsyncGenerator ->yield "a" : any +>(async function * () { yield "a"; })() : AsyncGenerator +>(async function * () { yield "a"; }) : () => AsyncGenerator +>async function * () { yield "a"; } : () => AsyncGenerator +>yield "a" : undefined >"a" : "a" } async function * explicitReturnType7(): AsyncIterator { @@ -201,10 +201,10 @@ async function * explicitReturnType9(): AsyncIterator { yield* (async function * () { yield "a"; })(); >yield* (async function * () { yield "a"; })() : void ->(async function * () { yield "a"; })() : AsyncGenerator ->(async function * () { yield "a"; }) : () => AsyncGenerator ->async function * () { yield "a"; } : () => AsyncGenerator ->yield "a" : any +>(async function * () { yield "a"; })() : AsyncGenerator +>(async function * () { yield "a"; }) : () => AsyncGenerator +>async function * () { yield "a"; } : () => AsyncGenerator +>yield "a" : undefined >"a" : "a" } async function * explicitReturnType10(): IterableIterator { diff --git a/tests/cases/compiler/contextualReturnTypeOfIIFE.ts b/tests/cases/compiler/contextualReturnTypeOfIIFE.ts new file mode 100644 index 0000000000000..b4497cdd4ec10 --- /dev/null +++ b/tests/cases/compiler/contextualReturnTypeOfIIFE.ts @@ -0,0 +1,13 @@ +// @lib: esnext + +const test1: Promise<[one: number, two: string]> = (async () => { + return [1, 'two']; +})(); + +const test2: Promise<[one: number, two: string]> = new Promise( + (resolve) => resolve([1, 'two']), +); + +const obj: { foo: [one: number, two: string] } = { + foo: (() => [1, 'two'])() +};