Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
SyMind opened this issue Sep 1, 2020 · 1 comment · Fixed by #44730
Closed

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

SyMind opened this issue Sep 1, 2020 · 1 comment · Fixed by #44730
Labels
Duplicate An existing issue was already created

Comments

@SyMind
Copy link

SyMind commented Sep 1, 2020

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.

@MartinJohns
Copy link
Contributor

And another duplicate of #12184.

use TS 3.3 (latest)

Uhh... The latest version is 4.0.2, not 3.3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants