Skip to content

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

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

Activity

  1. gcanti commented on Feb 9, 2017

    @gcanti

    Minimal repro

    const f = () => 'a' // inferred type f: () => string
    const v: 'a' = f()  // Type 'string' is not assignable to type '"a"'
  2. ahejlsberg commented on Feb 10, 2017

    @ahejlsberg
    Member

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

  3. locked and limited conversation to collaborators on Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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