Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Non-widening literal type inferences #24310

Merged
merged 5 commits into from
May 22, 2018
Merged

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.

2 participants