diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 8f8be4a10ef..69a46ed6cb2 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -19686,7 +19686,7 @@ namespace ts { return undefined; } - return typeAsAwaitable.awaitedTypeOfType = getUnionType(types, /*subtypeReduction*/ true); + return typeAsAwaitable.awaitedTypeOfType = getUnionType(types); } const promisedType = getPromisedTypeOfPromise(type); diff --git a/tests/baselines/reference/awaitUnionPromise.js b/tests/baselines/reference/awaitUnionPromise.js new file mode 100644 index 00000000000..df4c937fc63 --- /dev/null +++ b/tests/baselines/reference/awaitUnionPromise.js @@ -0,0 +1,89 @@ +//// [awaitUnionPromise.ts] +/// @target: es2015 +// https://github.com/Microsoft/TypeScript/issues/18186 + +class AsyncEnumeratorDone { }; + +interface IAsyncEnumerator { + next1(): Promise; + next2(): Promise | Promise; + next3(): Promise; + next4(): Promise; +} + +async function main() { + const x: IAsyncEnumerator = null; + let a = await x.next1(); + let b = await x.next2(); + let c = await x.next3(); + let d = await x.next4(); +} + +//// [awaitUnionPromise.js] +/// @target: es2015 +// https://github.com/Microsoft/TypeScript/issues/18186 +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + 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) : new P(function (resolve) { resolve(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 = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t; + if (y = 0, t) op = [0, 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 AsyncEnumeratorDone = /** @class */ (function () { + function AsyncEnumeratorDone() { + } + return AsyncEnumeratorDone; +}()); +; +function main() { + return __awaiter(this, void 0, void 0, function () { + var x, a, b, c, d; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: + x = null; + return [4 /*yield*/, x.next1()]; + case 1: + a = _a.sent(); + return [4 /*yield*/, x.next2()]; + case 2: + b = _a.sent(); + return [4 /*yield*/, x.next3()]; + case 3: + c = _a.sent(); + return [4 /*yield*/, x.next4()]; + case 4: + d = _a.sent(); + return [2 /*return*/]; + } + }); + }); +} diff --git a/tests/baselines/reference/awaitUnionPromise.symbols b/tests/baselines/reference/awaitUnionPromise.symbols new file mode 100644 index 00000000000..fb26ba1bfac --- /dev/null +++ b/tests/baselines/reference/awaitUnionPromise.symbols @@ -0,0 +1,67 @@ +=== tests/cases/compiler/awaitUnionPromise.ts === +/// @target: es2015 +// https://github.com/Microsoft/TypeScript/issues/18186 + +class AsyncEnumeratorDone { }; +>AsyncEnumeratorDone : Symbol(AsyncEnumeratorDone, Decl(awaitUnionPromise.ts, 0, 0)) + +interface IAsyncEnumerator { +>IAsyncEnumerator : Symbol(IAsyncEnumerator, Decl(awaitUnionPromise.ts, 3, 30)) +>T : Symbol(T, Decl(awaitUnionPromise.ts, 5, 27)) + + next1(): Promise; +>next1 : Symbol(IAsyncEnumerator.next1, Decl(awaitUnionPromise.ts, 5, 31)) +>Promise : Symbol(Promise, Decl(lib.d.ts, --, --)) +>T : Symbol(T, Decl(awaitUnionPromise.ts, 5, 27)) +>AsyncEnumeratorDone : Symbol(AsyncEnumeratorDone, Decl(awaitUnionPromise.ts, 0, 0)) + + next2(): Promise | Promise; +>next2 : Symbol(IAsyncEnumerator.next2, Decl(awaitUnionPromise.ts, 6, 46)) +>Promise : Symbol(Promise, Decl(lib.d.ts, --, --)) +>T : Symbol(T, Decl(awaitUnionPromise.ts, 5, 27)) +>Promise : Symbol(Promise, Decl(lib.d.ts, --, --)) +>AsyncEnumeratorDone : Symbol(AsyncEnumeratorDone, Decl(awaitUnionPromise.ts, 0, 0)) + + next3(): Promise; +>next3 : Symbol(IAsyncEnumerator.next3, Decl(awaitUnionPromise.ts, 7, 55)) +>Promise : Symbol(Promise, Decl(lib.d.ts, --, --)) +>T : Symbol(T, Decl(awaitUnionPromise.ts, 5, 27)) + + next4(): Promise; +>next4 : Symbol(IAsyncEnumerator.next4, Decl(awaitUnionPromise.ts, 8, 29)) +>Promise : Symbol(Promise, Decl(lib.d.ts, --, --)) +>T : Symbol(T, Decl(awaitUnionPromise.ts, 5, 27)) +>x : Symbol(x, Decl(awaitUnionPromise.ts, 9, 26)) +} + +async function main() { +>main : Symbol(main, Decl(awaitUnionPromise.ts, 10, 1)) + + const x: IAsyncEnumerator = null; +>x : Symbol(x, Decl(awaitUnionPromise.ts, 13, 9)) +>IAsyncEnumerator : Symbol(IAsyncEnumerator, Decl(awaitUnionPromise.ts, 3, 30)) + + let a = await x.next1(); +>a : Symbol(a, Decl(awaitUnionPromise.ts, 14, 7)) +>x.next1 : Symbol(IAsyncEnumerator.next1, Decl(awaitUnionPromise.ts, 5, 31)) +>x : Symbol(x, Decl(awaitUnionPromise.ts, 13, 9)) +>next1 : Symbol(IAsyncEnumerator.next1, Decl(awaitUnionPromise.ts, 5, 31)) + + let b = await x.next2(); +>b : Symbol(b, Decl(awaitUnionPromise.ts, 15, 7)) +>x.next2 : Symbol(IAsyncEnumerator.next2, Decl(awaitUnionPromise.ts, 6, 46)) +>x : Symbol(x, Decl(awaitUnionPromise.ts, 13, 9)) +>next2 : Symbol(IAsyncEnumerator.next2, Decl(awaitUnionPromise.ts, 6, 46)) + + let c = await x.next3(); +>c : Symbol(c, Decl(awaitUnionPromise.ts, 16, 7)) +>x.next3 : Symbol(IAsyncEnumerator.next3, Decl(awaitUnionPromise.ts, 7, 55)) +>x : Symbol(x, Decl(awaitUnionPromise.ts, 13, 9)) +>next3 : Symbol(IAsyncEnumerator.next3, Decl(awaitUnionPromise.ts, 7, 55)) + + let d = await x.next4(); +>d : Symbol(d, Decl(awaitUnionPromise.ts, 17, 7)) +>x.next4 : Symbol(IAsyncEnumerator.next4, Decl(awaitUnionPromise.ts, 8, 29)) +>x : Symbol(x, Decl(awaitUnionPromise.ts, 13, 9)) +>next4 : Symbol(IAsyncEnumerator.next4, Decl(awaitUnionPromise.ts, 8, 29)) +} diff --git a/tests/baselines/reference/awaitUnionPromise.types b/tests/baselines/reference/awaitUnionPromise.types new file mode 100644 index 00000000000..f1d91c038de --- /dev/null +++ b/tests/baselines/reference/awaitUnionPromise.types @@ -0,0 +1,76 @@ +=== tests/cases/compiler/awaitUnionPromise.ts === +/// @target: es2015 +// https://github.com/Microsoft/TypeScript/issues/18186 + +class AsyncEnumeratorDone { }; +>AsyncEnumeratorDone : AsyncEnumeratorDone + +interface IAsyncEnumerator { +>IAsyncEnumerator : IAsyncEnumerator +>T : T + + next1(): Promise; +>next1 : () => Promise +>Promise : Promise +>T : T +>AsyncEnumeratorDone : AsyncEnumeratorDone + + next2(): Promise | Promise; +>next2 : () => Promise | Promise +>Promise : Promise +>T : T +>Promise : Promise +>AsyncEnumeratorDone : AsyncEnumeratorDone + + next3(): Promise; +>next3 : () => Promise<{} | T> +>Promise : Promise +>T : T + + next4(): Promise; +>next4 : () => Promise +>Promise : Promise +>T : T +>x : string +} + +async function main() { +>main : () => Promise + + const x: IAsyncEnumerator = null; +>x : IAsyncEnumerator +>IAsyncEnumerator : IAsyncEnumerator +>null : null + + let a = await x.next1(); +>a : number | AsyncEnumeratorDone +>await x.next1() : number | AsyncEnumeratorDone +>x.next1() : Promise +>x.next1 : () => Promise +>x : IAsyncEnumerator +>next1 : () => Promise + + let b = await x.next2(); +>b : number | AsyncEnumeratorDone +>await x.next2() : number | AsyncEnumeratorDone +>x.next2() : Promise | Promise +>x.next2 : () => Promise | Promise +>x : IAsyncEnumerator +>next2 : () => Promise | Promise + + let c = await x.next3(); +>c : number | {} +>await x.next3() : number | {} +>x.next3() : Promise +>x.next3 : () => Promise +>x : IAsyncEnumerator +>next3 : () => Promise + + let d = await x.next4(); +>d : number | { x: string; } +>await x.next4() : number | { x: string; } +>x.next4() : Promise +>x.next4 : () => Promise +>x : IAsyncEnumerator +>next4 : () => Promise +} diff --git a/tests/cases/compiler/awaitUnionPromise.ts b/tests/cases/compiler/awaitUnionPromise.ts new file mode 100644 index 00000000000..2c8470e97a6 --- /dev/null +++ b/tests/cases/compiler/awaitUnionPromise.ts @@ -0,0 +1,19 @@ +/// @target: es2015 +// https://github.com/Microsoft/TypeScript/issues/18186 + +class AsyncEnumeratorDone { }; + +interface IAsyncEnumerator { + next1(): Promise; + next2(): Promise | Promise; + next3(): Promise; + next4(): Promise; +} + +async function main() { + const x: IAsyncEnumerator = null; + let a = await x.next1(); + let b = await x.next2(); + let c = await x.next3(); + let d = await x.next4(); +} \ No newline at end of file