Skip to content

TS2345 error with discriminated unions #17480

Closed
@webartoli

Description

@webartoli

TypeScript Version: 2.4.2

Code

interface AddTodoAction {
  type: 'ADD_TODO'
  id: number
  text: string
}

interface ToggleTodoAction {
  type: 'TOGGLE_TODO'
  id: number
}

type TodosAction = AddTodoAction | ToggleTodoAction

const action = {
  type: 'TOGGLE_TODO',
  id: 1
}

const target: TodosAction = action
      ^^^^^^

// or

const fun = (a: TodosAction) => {

}

fun(action)
    ^^^^^^

Expected behavior:

No error.

Actual behavior:

error TS2345: Argument of type '{ type: string; id: number; }' is not assignable to parameter of type 'TodosAction'.
  Type '{ type: string; id: number; }' is not assignable to type 'ToggleTodoAction'.
    Types of property 'type' are incompatible.
      Type 'string' is not assignable to type '"TOGGLE_TODO"'.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions