镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Wrong Type Inference when Using 3rd Variable #33192

Description

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:

Activity

  1. jack-williams commented on Sep 2, 2019

    @jack-williams
    Collaborator

    This is tracked at #12184.

  2. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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