Skip to content

Unexpect Object is possibly 'undefined' Error when checked before. #40341

Closed
@SyMind

Description

@SyMind

Steps

use TS 3.3 (latest)
enable all strict checks
have code such as this:

interface Props {
    foo?: {
        bar: string;
        baz: string;
    }
}

function hello({foo}: Props) {
    const isFooPresent = foo !== undefined;
    let bar = isFooPresent ? foo.bar : ''; // Object is possibly 'undefined'.
    let baz = isFooPresent ? foo.baz : ''; // Object is possibly 'undefined'.

    // but follow code is safe.
    //  let bar = foo !== undefined ? foo.bar : '';
    //  let baz = foo !== undefined ? foo.baz : '';
}

Using a const variable to store check result, an error when occur.

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