Skip to content

Type Gaurds Don't Narrow Type in Inner Functions #10927

Closed
@ethanresnick

Description

@ethanresnick

TypeScript Version: 2.0.2

Code

  function isNumber(test: number|string): test is number {
    return typeof test === "number";
  }
  function blah(arg: () => void): void { }

  let x: any = 4;

  if(isNumber(x)) {
    x; // x is correctly narrowed to number here
    blah(() => {
      x; // typeof x is any again
    })
  }

Expected behavior:
(I think) x should still be inferred as type number in the inner function.

Actual behavior:
x is inferred as any

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions