Skip to content

Narrowing down the type of a parameter isn't recognized in closure #34669

Description

@jmankopf

TypeScript Version: v3.7-Beta

Search terms : narrow narrowing down type parameter closure

Code

function foo(arg: string | undefined) {
    arg = arg ?? "default";

    arg.length // no error
    const bar = () => {
        arg.length; // error 
    }
}

Expected behavior:
arg should be of type string within the body of the closure.

Actual behavior:
arg is possibly 'undefined' within the body of the closure.

Playground Link: Playground link

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions