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

Activity

  1. MartinJohns commented on Oct 23, 2019

    @MartinJohns
    Contributor

    Duplicate of #9998.

  2. fatcerberus commented on Oct 24, 2019

    @fatcerberus

    Is this actually a duplicate of #9998? That's a design limitation wherein narrowings are optimistically not reset when calling a function because it would be impractical to follow the control flow into the call. Here, the narrowing is being reset at a closure boundary, which is normal (i.e. not just a design limitation - it's deliberate).

  3. Buntelrus commented on Nov 20, 2020

    @Buntelrus

    What's the conclusion of this?

  4. MartinJohns commented on Nov 20, 2020

    @MartinJohns
    Contributor

    The conclusion is that it's a duplicate of #9998.

  5. Buntelrus commented on Nov 20, 2020

    @Buntelrus

    I don't get it. What can I do to solve this problem?

  6. 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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions