Skip to content

Falsy instanceof check doesn't affect righthand or expressionΒ #55395

Closed
@LeviticusMB

Description

@LeviticusMB

πŸ”Ž Search Terms

instanceof or unknown

πŸ•— Version & Regression Information

  • Didn't think of it until 4.4 when caught values became unknown, but the actual problem has probably been present for a long time.

See #38309, for instance.

⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.2.0-beta#code/MYGwhgzhAEDqCmAjAogJ1Qe1deAPALvAHYAmMAqgEoCSam2A3gLABQ070E+Y+ArhAH4AXNCK8AtonioA3KwC+rVvlQBPaMzYcA9NugBZMAGt40MNFTwAjr3hcF0YD2AALaAAppqAJQbWHaABLADMPLyCiLjAiYHgMUIQUdCxoAF506GCwEAhTAB88nHQAOii+GABCVOgAFgAGAEZfTQCA4AxIjBB4Yq8sT3RvOS12RRYxoA

πŸ’» Code

class WebError extends URIError {
    status?: number;
}

try {
    // Make a request
} catch (err) {
    if (err instanceof WebError === false || err.status != 401) {
        console.error(err);
    }
}

πŸ™ Actual behavior

The compiler thinks err is unknown and does not understand that err.status must be a valid property.

πŸ™‚ Expected behavior

No compiler error.

Additional information about the issue

No response

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