Skip to content
Merged
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25309,6 +25309,10 @@ namespace ts {
if (signature && !isResolvingReturnTypeOfSignature(signature)) {
return getReturnTypeOfSignature(signature);
}
const iife = getImmediatelyInvokedFunctionExpression(functionDecl);
if (iife) {
return getContextualType(iife);
}
return undefined;
}

Expand Down
61 changes: 61 additions & 0 deletions tests/baselines/reference/contextualReturnTypeOfIIFE.js
Original file line number Diff line number Diff line change
@@ -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']; })()
};
28 changes: 28 additions & 0 deletions tests/baselines/reference/contextualReturnTypeOfIIFE.symbols
Original file line number Diff line number Diff line change
@@ -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))

};

46 changes: 46 additions & 0 deletions tests/baselines/reference/contextualReturnTypeOfIIFE.types
Original file line number Diff line number Diff line change
@@ -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"

};

2 changes: 2 additions & 0 deletions tests/baselines/reference/generatorTypeCheck27.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -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, --, --))

} ();
}
16 changes: 8 additions & 8 deletions tests/baselines/reference/generatorTypeCheck27.types
Original file line number Diff line number Diff line change
Expand Up @@ -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

} ();
}
2 changes: 2 additions & 0 deletions tests/baselines/reference/generatorTypeCheck29.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ function* g2(): Iterator<Iterable<(x: string) => 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, --, --))

} ()
}
16 changes: 8 additions & 8 deletions tests/baselines/reference/generatorTypeCheck29.types
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ 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: 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

} ()
}
2 changes: 2 additions & 0 deletions tests/baselines/reference/generatorTypeCheck30.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ function* g2(): Iterator<Iterable<(x: string) => 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, --, --))

} ()
}
16 changes: 8 additions & 8 deletions tests/baselines/reference/generatorTypeCheck30.types
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ 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: 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

} ()
}
8 changes: 4 additions & 4 deletions tests/baselines/reference/generatorTypeCheck31.errors.txt
Original file line number Diff line number Diff line change
@@ -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) ====
Expand All @@ -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>'.
}
4 changes: 2 additions & 2 deletions tests/baselines/reference/generatorTypeCheck31.types
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
54 changes: 27 additions & 27 deletions tests/baselines/reference/types.asyncGenerators.es2018.1.types
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,14 @@ const assignability4: () => AsyncIterableIterator<number> = async function * ()
};
const assignability5: () => AsyncIterableIterator<number> = async function * () {
>assignability5 : () => AsyncIterableIterator<number>
>async function * () { yield* (async function * () { yield 1; })();} : () => AsyncGenerator<number, void, unknown>
>async function * () { yield* (async function * () { yield 1; })();} : () => AsyncGenerator<number, void, undefined>

yield* (async function * () { yield 1; })();
>yield* (async function * () { yield 1; })() : void
>(async function * () { yield 1; })() : AsyncGenerator<number, void, unknown>
>(async function * () { yield 1; }) : () => AsyncGenerator<number, void, unknown>
>async function * () { yield 1; } : () => AsyncGenerator<number, void, unknown>
>yield 1 : any
>(async function * () { yield 1; })() : AsyncGenerator<number, void, undefined>
>(async function * () { yield 1; }) : () => AsyncGenerator<number, void, undefined>
>async function * () { yield 1; } : () => AsyncGenerator<number, void, undefined>
>yield 1 : undefined
>1 : 1

};
Expand Down Expand Up @@ -182,14 +182,14 @@ const assignability9: () => AsyncIterable<number> = async function * () {
};
const assignability10: () => AsyncIterable<number> = async function * () {
>assignability10 : () => AsyncIterable<number>
>async function * () { yield* (async function * () { yield 1; })();} : () => AsyncGenerator<number, void, unknown>
>async function * () { yield* (async function * () { yield 1; })();} : () => AsyncGenerator<number, void, undefined>

yield* (async function * () { yield 1; })();
>yield* (async function * () { yield 1; })() : void
>(async function * () { yield 1; })() : AsyncGenerator<number, void, unknown>
>(async function * () { yield 1; }) : () => AsyncGenerator<number, void, unknown>
>async function * () { yield 1; } : () => AsyncGenerator<number, void, unknown>
>yield 1 : any
>(async function * () { yield 1; })() : AsyncGenerator<number, void, undefined>
>(async function * () { yield 1; }) : () => AsyncGenerator<number, void, undefined>
>async function * () { yield 1; } : () => AsyncGenerator<number, void, undefined>
>yield 1 : undefined
>1 : 1

};
Expand Down Expand Up @@ -242,14 +242,14 @@ const assignability14: () => AsyncIterator<number> = async function * () {
};
const assignability15: () => AsyncIterator<number> = async function * () {
>assignability15 : () => AsyncIterator<number>
>async function * () { yield* (async function * () { yield 1; })();} : () => AsyncGenerator<number, void, unknown>
>async function * () { yield* (async function * () { yield 1; })();} : () => AsyncGenerator<number, void, undefined>

yield* (async function * () { yield 1; })();
>yield* (async function * () { yield 1; })() : void
>(async function * () { yield 1; })() : AsyncGenerator<number, void, unknown>
>(async function * () { yield 1; }) : () => AsyncGenerator<number, void, unknown>
>async function * () { yield 1; } : () => AsyncGenerator<number, void, unknown>
>yield 1 : any
>(async function * () { yield 1; })() : AsyncGenerator<number, void, undefined>
>(async function * () { yield 1; }) : () => AsyncGenerator<number, void, undefined>
>async function * () { yield 1; } : () => AsyncGenerator<number, void, undefined>
>yield 1 : undefined
>1 : 1

};
Expand Down Expand Up @@ -297,10 +297,10 @@ async function * explicitReturnType5(): AsyncIterableIterator<number> {

yield* (async function * () { yield 1; })();
>yield* (async function * () { yield 1; })() : void
>(async function * () { yield 1; })() : AsyncGenerator<number, void, unknown>
>(async function * () { yield 1; }) : () => AsyncGenerator<number, void, unknown>
>async function * () { yield 1; } : () => AsyncGenerator<number, void, unknown>
>yield 1 : any
>(async function * () { yield 1; })() : AsyncGenerator<number, void, undefined>
>(async function * () { yield 1; }) : () => AsyncGenerator<number, void, undefined>
>async function * () { yield 1; } : () => AsyncGenerator<number, void, undefined>
>yield 1 : undefined
>1 : 1
}
async function * explicitReturnType6(): AsyncIterable<number> {
Expand Down Expand Up @@ -347,10 +347,10 @@ async function * explicitReturnType10(): AsyncIterable<number> {

yield* (async function * () { yield 1; })();
>yield* (async function * () { yield 1; })() : void
>(async function * () { yield 1; })() : AsyncGenerator<number, void, unknown>
>(async function * () { yield 1; }) : () => AsyncGenerator<number, void, unknown>
>async function * () { yield 1; } : () => AsyncGenerator<number, void, unknown>
>yield 1 : any
>(async function * () { yield 1; })() : AsyncGenerator<number, void, undefined>
>(async function * () { yield 1; }) : () => AsyncGenerator<number, void, undefined>
>async function * () { yield 1; } : () => AsyncGenerator<number, void, undefined>
>yield 1 : undefined
>1 : 1
}
async function * explicitReturnType11(): AsyncIterator<number> {
Expand Down Expand Up @@ -397,10 +397,10 @@ async function * explicitReturnType15(): AsyncIterator<number> {

yield* (async function * () { yield 1; })();
>yield* (async function * () { yield 1; })() : void
>(async function * () { yield 1; })() : AsyncGenerator<number, void, unknown>
>(async function * () { yield 1; }) : () => AsyncGenerator<number, void, unknown>
>async function * () { yield 1; } : () => AsyncGenerator<number, void, unknown>
>yield 1 : any
>(async function * () { yield 1; })() : AsyncGenerator<number, void, undefined>
>(async function * () { yield 1; }) : () => AsyncGenerator<number, void, undefined>
>async function * () { yield 1; } : () => AsyncGenerator<number, void, undefined>
>yield 1 : undefined
>1 : 1
}
async function * explicitReturnType16(): {} {
Expand Down
Loading