Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong Type Inference when Using 3rd Variable #33192

Closed
mikeborozdin opened this issue Sep 2, 2019 · 1 comment · Fixed by #44730
Closed

Wrong Type Inference when Using 3rd Variable #33192

mikeborozdin opened this issue Sep 2, 2019 · 1 comment · Fixed by #44730
Labels
Duplicate An existing issue was already created

Comments

@mikeborozdin
Copy link

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:

@jack-williams
Copy link
Collaborator

This is tracked at #12184.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants