Skip to content

Wrong Type Inference when Using 3rd Variable #33192

Closed
@mikeborozdin

Description

@mikeborozdin

TypeScript Version: 3.5.2

Search Terms: undefined, function arguments, variable, if condition, static code analysis

Code

const foo = (bar: number) => {};

const someOtherFunction = (bar?: number) => {
  const shouldCallFoo = !!bar;

  if (shouldCallFoo) {
    foo(bar);
  }
}

Expected behavior:

The code compiles without any problems

Actual behavior:

Error:

Argument of type 'number | undefined' is not assignable to parameter of type 'number'.
  Type 'undefined' is not assignable to type 'number'.

Playground Link: http://www.typescriptlang.org/play/#code/MYewdgzgLgBAZiEMC8MAUAjAhgJwFwxgCuAthgKY4CUKAfDAN4C+A3AFBuiSwQgnkB5KAAtKAMSJhgUAJbgU6bDgD8BYmUo1k9Bmxj6YXaDAjCQRADYATAMJYLFsYgVL2egzLjpT563YdOIDS6BqHwiJi4VOyhTGxMQA

Related Issues:

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