Closed
Description
TypeScript Version: 3.6.2
Search Terms: regression, string literal, union, 3.6, inference, generic.
Code
type NetworkProtocol = "TCP";
type Input<T> = Promise<T> | T;
function ifUndefined<T>(input:Input<T> | undefined, value: Input<T>): Promise<T> {
throw new Error();
}
const y = ifUndefined(new Promise<NetworkProtocol>(() => {}), "TCP")
const z: Promise<NetworkProtocol> = y;
Expected behavior:
No error (3.5 and earlier).
Actual behavior:
A type error (3.6.2):
Type 'Promise<string>' is not assignable to type 'Promise<NetworkProtocol>'.
Type 'string' is not assignable to type 'NetworkProtocol'.
Playground Link: Playground. The TypeScript Playground doesn't appear to yet use 3.6.2 though so this doesn't repro in the playground.
Note that this example does work as expected - and looks morally "the same":
type NetworkProtocol = "TCP";
function ifUndefined<T>(input:Promise<T> | undefined, value: T): Promise<T> {
throw new Error();
}
const y = ifUndefined(new Promise<NetworkProtocol>(() => {}), "TCP")
const z: Promise<NetworkProtocol> = y;