Skip to content

Truthy check loses effect based on position in "&&" chain #36870

Closed
@cakoose

Description

@cakoose

I have three checks in a "&&" chain. The order of the first two checks seems to have an effect on whether I can perform the third check. Am I missing something?

function f1(x: unknown): any {
    return x && typeof x === 'object' && x.hasOwnProperty('x');
    //                                   ^  TS reports error: Object is possibly 'null'
}

function f2(x: unknown): any {
    return typeof x === 'object' && x && x.hasOwnProperty('x');  // no error
}

Reproduced in 3.8-beta (playground link)

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions