Skip to content

Destructuring breaks tagged union case inference #23613

Closed
@bcherny

Description

@bcherny

TypeScript Version: 2.8.3

Search Terms: union, destructure

Code

type Message =
    { type: 'sendMessageToThread', data: { threadId: number, message: string }}
  | { type: 'createThread', data: { userIds: number[] }}
  | { type: 'addUserToThread', data: { threadId: number, userId: number }}

// Good case
function a(message: Message) {
  switch (message.type) {
    case 'sendMessageToThread':
      console.log(message.data.threadId)
  }
}

// Bad case
function b({ type, data }: Message) {
  switch (type) {
    case 'sendMessageToThread':
      // Error: Property 'threadId' does not exist on type
      console.log(data.threadId)
  }
}

Expected behavior:

TS should infer which case the tagged union falls into regardless of whether or not the parameter is destructured. data should be inferred as { threadId: number, message: string }.

Actual behavior:

data is inferred as the union of all data values in Message.

Playground Link

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