Skip to content

Narrowing behaviour differs with guard order #7045

Closed
@yortus

Description

@yortus

Whilst looking at @RyanCavanaugh's example code in this comment I noticed odd narrowing behaviour depending how the type guards are arranged. Is this expected?

function isActuallyThing(x: any): x is Thing {
  return /* my special way of determining Thingness */
}

function doSomething(x: Thing|string) {
  if (typeof x === 'string') {
    //...
  } else if (isActuallyThing(x)) {
    //...
  } else {
    x // type of x here is Thing               <== WHY NOT Thing|string?
  }
}


function sameButDifferent(x: Thing|string) {
  if (isActuallyThing(x)) {
    //...
  }
  else if (typeof x === 'string') {
    //...
  } else {
    x // type of x here is Thing|string
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    By DesignDeprecated - use "Working as Intended" or "Design Limitation" instead

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions