Skip to content

nullish types not narrowed in if blockΒ #64229

Description

@errorx666

πŸ”Ž Search Terms

null nullish undefined narrow if

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about null

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=6.0.3#code/JYOwLgpgTgZghgYwgAgCJzHZBvAsAKGSOSggGcBXAGzAGUIwB+ALhwOI+WigHsoyWJCHAAmPEFQCeyMmCigA5gG0AusgA+yENSoBudsQC+GrTv35DBAnDKSQCZDAr2wwccgUN0mABQBKVgAFXgBbYDIIAB5vOHVtKioAPhwhMAooEFME3WRLfGtbe0dnBFd3CAAPOBCAByoIfzZCYgRxWWQRDCwAXmQ4AHc4YDAPLy7-cw5gGGQfH07MRgA6UkoaeiYl7j4BJfqQBTAAC2RGRmQABj9kZKumzmIAekeOrpXyajoGLahefmQEHAQAByEYAIxQ8SoyCO0AgABo+mRZgs4MtVp8NsttvxlvtDiczpdrrcDA9WiAyDx6j8-j5Xph3msvmBaTslgArHigenAgA6IOQ1z8kyMBDyBEq1TqDRFBCAA

πŸ’» Code

interface Data {
    resultSet?: {
        errors?: readonly string[] | null;
    } | null;
}

async function getData(): Promise<Data|null> { return null; }

async function example() {
    const data = await getData();
    if ((data?.resultSet?.errors?.length ?? 0) > 0) {
        // data.resultSet.errors can't be null here, as (data?.resultSet?.errors?.length ?? 0) > 0
        console.error( data.resultSet.errors.join( '\n' ) );
    }
}

example();

πŸ™ Actual behavior

Errors in code

'data' is possibly 'null'.
'data.resultSet' is possibly 'null' or 'undefined'.
'data.resultSet.errors' is possibly 'null' or 'undefined'.

πŸ™‚ Expected behavior

No errors

Additional information about the issue

The if condition should eliminate nullish values from the body.

Activity

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