Skip to content

Non-widening literal type inferences - #24310

Merged
Anders Hejlsberg (ahejlsberg) merged 5 commits into
masterfrom
nonWideningLiteralInferences
May 22, 2018
Merged

Non-widening literal type inferences#24310
Anders Hejlsberg (ahejlsberg) merged 5 commits into
masterfrom
nonWideningLiteralInferences

Conversation

@ahejlsberg

Copy link
Copy Markdown
Member

With this PR we infer non-widening literal types (see #11126) when making inferences for a type parameter that includes at least one primitive type in its constraint. The rationale is that a primitive type in the constraint indicates that the intended target is a subtype of the primitive type, i.e. a literal type.

declare function widening<T>(x: T): T;
declare function nonWidening<T extends string | number | symbol>(x: T): T;

let x1 = widening('a');  // string
let x2 = widening(10);  // number
let x3 = widening(cond ? 'a' : 10);  // string | number
let y1 = nonWidening('a');  // "a"
let y2 = nonWidening(10);  // 10
let y3 = nonWidening(cond ? 'a' : 10);  // "a" | 10

Fixes #23649.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

2 participants