Skip to content

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

Closed
@na-o-ys

Description

@na-o-ys

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'.

Metadata

Metadata

Assignees

No one assigned

    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