Do not reduce subtypes of awaited union type

This commit is contained in:
Ron Buckton 2017-10-16 14:26:16 -07:00
parent 22769d95e1
commit 9563246993
5 changed files with 252 additions and 1 deletions

View file

@ -19680,7 +19680,7 @@ namespace ts {
return undefined;
}
return typeAsAwaitable.awaitedTypeOfType = getUnionType(types, /*subtypeReduction*/ true);
return typeAsAwaitable.awaitedTypeOfType = getUnionType(types);
}
const promisedType = getPromisedTypeOfPromise(type);

View file

@ -0,0 +1,89 @@
//// [awaitUnionPromise.ts]
/// @target: es2015
// https://github.com/Microsoft/TypeScript/issues/18186
class AsyncEnumeratorDone { };
interface IAsyncEnumerator<T> {
next1(): Promise<T | AsyncEnumeratorDone>;
next2(): Promise<T> | Promise<AsyncEnumeratorDone>;
next3(): Promise<T | {}>;
next4(): Promise<T | { x: string }>;
}
async function main() {
const x: IAsyncEnumerator<number> = 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*/];
}
});
});
}

View file

@ -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<T> {
>IAsyncEnumerator : Symbol(IAsyncEnumerator, Decl(awaitUnionPromise.ts, 3, 30))
>T : Symbol(T, Decl(awaitUnionPromise.ts, 5, 27))
next1(): Promise<T | AsyncEnumeratorDone>;
>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<T> | Promise<AsyncEnumeratorDone>;
>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<T | {}>;
>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<T | { x: string }>;
>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<number> = 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))
}

View file

@ -0,0 +1,76 @@
=== tests/cases/compiler/awaitUnionPromise.ts ===
/// @target: es2015
// https://github.com/Microsoft/TypeScript/issues/18186
class AsyncEnumeratorDone { };
>AsyncEnumeratorDone : AsyncEnumeratorDone
interface IAsyncEnumerator<T> {
>IAsyncEnumerator : IAsyncEnumerator<T>
>T : T
next1(): Promise<T | AsyncEnumeratorDone>;
>next1 : () => Promise<AsyncEnumeratorDone | T>
>Promise : Promise<T>
>T : T
>AsyncEnumeratorDone : AsyncEnumeratorDone
next2(): Promise<T> | Promise<AsyncEnumeratorDone>;
>next2 : () => Promise<T> | Promise<AsyncEnumeratorDone>
>Promise : Promise<T>
>T : T
>Promise : Promise<T>
>AsyncEnumeratorDone : AsyncEnumeratorDone
next3(): Promise<T | {}>;
>next3 : () => Promise<{} | T>
>Promise : Promise<T>
>T : T
next4(): Promise<T | { x: string }>;
>next4 : () => Promise<T | { x: string; }>
>Promise : Promise<T>
>T : T
>x : string
}
async function main() {
>main : () => Promise<void>
const x: IAsyncEnumerator<number> = null;
>x : IAsyncEnumerator<number>
>IAsyncEnumerator : IAsyncEnumerator<T>
>null : null
let a = await x.next1();
>a : number | AsyncEnumeratorDone
>await x.next1() : number | AsyncEnumeratorDone
>x.next1() : Promise<number | AsyncEnumeratorDone>
>x.next1 : () => Promise<number | AsyncEnumeratorDone>
>x : IAsyncEnumerator<number>
>next1 : () => Promise<number | AsyncEnumeratorDone>
let b = await x.next2();
>b : number | AsyncEnumeratorDone
>await x.next2() : number | AsyncEnumeratorDone
>x.next2() : Promise<AsyncEnumeratorDone> | Promise<number>
>x.next2 : () => Promise<AsyncEnumeratorDone> | Promise<number>
>x : IAsyncEnumerator<number>
>next2 : () => Promise<AsyncEnumeratorDone> | Promise<number>
let c = await x.next3();
>c : number | {}
>await x.next3() : number | {}
>x.next3() : Promise<number | {}>
>x.next3 : () => Promise<number | {}>
>x : IAsyncEnumerator<number>
>next3 : () => Promise<number | {}>
let d = await x.next4();
>d : number | { x: string; }
>await x.next4() : number | { x: string; }
>x.next4() : Promise<number | { x: string; }>
>x.next4 : () => Promise<number | { x: string; }>
>x : IAsyncEnumerator<number>
>next4 : () => Promise<number | { x: string; }>
}

View file

@ -0,0 +1,19 @@
/// @target: es2015
// https://github.com/Microsoft/TypeScript/issues/18186
class AsyncEnumeratorDone { };
interface IAsyncEnumerator<T> {
next1(): Promise<T | AsyncEnumeratorDone>;
next2(): Promise<T> | Promise<AsyncEnumeratorDone>;
next3(): Promise<T | {}>;
next4(): Promise<T | { x: string }>;
}
async function main() {
const x: IAsyncEnumerator<number> = null;
let a = await x.next1();
let b = await x.next2();
let c = await x.next3();
let d = await x.next4();
}