Skip to content

Conversation

@ahejlsberg
Copy link
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.

@ahejlsberg ahejlsberg merged commit f52c4af into master May 22, 2018
@ahejlsberg ahejlsberg deleted the nonWideningLiteralInferences branch May 22, 2018 13:56
@mhegazy mhegazy mentioned this pull request May 22, 2018
@microsoft microsoft locked and limited conversation to collaborators Jul 31, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants