Skip to content

False positive: Right operand of ?? is unreachable #60614

Closed
@steelbrain

Description

@steelbrain

🔎 Search Terms

Ever since Typescript 5.6, we've been getting false positives of the nullish operand lint rule. Our codebase is littered with it so it blocks us from upgrading to the latest Typescript

🕗 Version & Regression Information

  • This changed between versions 5.5 and 5.6 (also there on latest 5.7.2)

⏯ Playground Link

https://www.typescriptlang.org/play/?#code/GYVwdgxgLglg9mABADwDwBUB8AKAzlAJxjAHNcAuRdAUwFsAHAGwEMpqBlQ4sgQQIOYBPADSIAdBIBuzRiGoVEYELQBG1AgG0AugEpK6RAB9FIRo0QBvAFCJbiAtSggCSJWcTNci6pPU27APQBiAB6iIJwIIgQzEgyuHD2dHC+iFAAFtQeKilZjMTUouAOLGwAJmmJGVkqICSiCIyCaZkOlYi4IPT0Dri4-rZBHkjF1KXUFbUkYlYAvlZWQwAicPKKcFCIAO5wBADWlAAy1MCbuDBlWTBeYD7qJmbX6VYQCPiI0rLUlEqq9wC8KFQvzUBEwAAN0jBwYgAPywxAABgWQwA6rs9rhYS83ptPnIAEyIQFoEHqCFQ8E4sDvfHUADMP2UoOJHxkhLhCORQA

💻 Code

function x<T>(strings: TemplateStringsArray, ...values: number[]): T | null {
    return null as never
    // ^ you can also remove the above line, unrelated to the bug, only there to suppress
    // an unrelated error.
}

// Does not work: Left side is never nullish
const value: number = x<number>`hi` ?? 0
// ^ this line causes an error in Typescript

// Works?
const value2 = x<number>`hi`
const value3: number = value2 ?? 0

🙁 Actual behavior

Right operand of ?? is unreachable because the left operand is never nullish

🙂 Expected behavior

No error

Additional information about the issue

I tried looking for duplicate issues but wasn't able to find any. If the bug has already been filed, a link to it would be appreciated. Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScript

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions