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

Wrong return type inference with the Union of Literal types #13972

Closed
na-o-ys opened this issue Feb 9, 2017 · 2 comments
Closed

Wrong return type inference with the Union of Literal types #13972

na-o-ys opened this issue Feb 9, 2017 · 2 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@na-o-ys
Copy link

na-o-ys commented Feb 9, 2017

TypeScript Version: 2.2.0

Code

type T = 'a' | 'b'

const f1 = () => true ? 'a' : 'b'
const f2 = () => true ? 'a' : 'a'

const v1: T = f1()
const v2: T = f2()

Expected behavior:

Both will be ok.

Actual behavior:

Only the former is ok.

index.ts(7,7): error TS2322: Type 'string' is not assignable to type 'T'.
@gcanti
Copy link

gcanti commented Feb 9, 2017

Minimal repro

const f = () => 'a' // inferred type f: () => string
const v: 'a' = f()  // Type 'string' is not assignable to type '"a"'

@ahejlsberg
Copy link
Member

This is working as intended. See discussion here for the rationale.

@ahejlsberg ahejlsberg added the Working as Intended The behavior described is the intended behavior; this is not a bug label Feb 10, 2017
@mhegazy mhegazy closed this as completed Feb 28, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

4 participants