Skip to content

TS 3.2.0-rc seems to forget union type refinements more eagerly #28568

Closed
@Jessidhia

Description

@Jessidhia

TypeScript Version: 3.2.0-rc

Search Terms:
refinement

Code

(EDIT: incorrect reproduction, see correct example at #28568 (comment))

function test(x: { type: 'a'; body: string | null } | { type: 'b'; body: number | null }): string {
  if (x.body === null) {
    return 'null'
  }
  x.body.toString() // ok

  if (x.type === 'a') {
    return x.body.toString() // x.body may be null
  } else {
    return x.body.toString() // x.body may be null
  }
}

Expected behavior:
The refinement of x.body should persist (it does in 3.1)

Actual behavior:
TS forgets that it has already refined x.body to be not null.

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