Skip to content

Change in inferred type in 3.6 for function call using generic union instantiated at string literal  #33227

Closed
@lukehoban

Description

@lukehoban

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;

Metadata

Metadata

Assignees

Labels

Working as IntendedThe behavior described is the intended behavior; this is not a bug

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions