Skip to content

'Variable' is possibly 'undefined'. ts(18048)Β #60448

@g1tc4t

Description

@g1tc4t

πŸ”Ž Search Terms

Possibly undefined compiler checks

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, including 5.8.0 nightly, and checked the FAQ. I did not find anything about this error.

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.8.0-dev.20241107#code/JYOwLgpgTgZghgYwgAgJIBUCeAHFBvAKGWOWAC5kQBXAWwCNoBuAgXwIJipATGAHsQySAGcwACgBuARgrUANnOQAfZFwAmEGKAhrlaLLgA0yCQCZZVBXvWbtulRhwQAlBTp8+ciHBAFCJZAB6QNIYZHcwAAsTKWQfXTM4qBQbLRAdYwBHKmAwZEjoCCISYDDJWIBeKtUQDTSdZAAyRpNTZCqKmrq7Z2R-AJJksCooQTAoKghmALZi4mDQuJBMZD4w6VWoVtJhLtt0tWN4oQLVqOgdyj4wOaCQgHcIAHJk5Gzc3hAAc3zC42E+OEIHI+PcTsBdnJtJQILkClsNiA+Ai2ggfEC9vU1LdSshysgAITVMy9foDZBDEaCeByYRTW6zAILLRySBbPhUPK4nwrCRwKDAOB0LyXeRyHFlDYdSiWRRKFSJaVi0m3AKU0bIGl06YkRmDCDDDXSAB0wHaxNMpuYLCAA

πŸ’» Code

interface IType {
    i: number;
}

function test(v1: null | undefined | IType, v2: null | undefined | IType): boolean
{
    // if both v1 and v2 are undefined, quit here
    if (v1 === undefined && v2 === undefined) {
        return true;
    }

    // if any of v1 or v2 is undefined, and the other is not
    // we're quitting here, so below this line neither v1 nor v2 can be undefined
    if (v1 !== v2) {
        return false;
    }

    // filter out if any variable is null
    if (v1 === null || v2 === null) {
        return false;
    }

    return v1.i === v2.i;
}

πŸ™ Actual behavior

In the last code line the compilers throws an error, that v1 and v2 both could be undefined, but as commented in the code section they never will be undefined here.

πŸ™‚ Expected behavior

Do not throw errors like 'v1' is possibly 'undefined'. ts(18048)

Additional information about the issue

No response

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