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

Typescript 3.3.1 wrongly considers object as 'Possibly 'undefined'' #29805

Closed
vladimir-djokic opened this issue Feb 7, 2019 · 4 comments
Closed
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@vladimir-djokic
Copy link

TypeScript Version: 3.3.1

Issue: Even if the code is explicitly checking against variable not being undefined the compiler is complaining about Object is possibly 'undefined'. ts(2532) Strict checking is turned on with "strict": true.

ts_issue_001

ts_issue_002

@j-oliveras
Copy link
Contributor

j-oliveras commented Feb 7, 2019

Duplicated/related to #9998.

Basically, typescript don't know if the predicate is called synchronously or not.

@vladimir-djokic
Copy link
Author

vladimir-djokic commented Feb 7, 2019

Are you talking about find -> predicate function or the outer function of this code? Can you explain why this code compiles:

if (objectToMoveParentPathComponent !== undefined) {

    const c = objectToMoveParentPathComponent;

    const objectToMoveParent = objectToMoveResult.parents.find(
        p => p.parentObjectId === c.objectId // No error
    );

    // ...

}

@j-oliveras
Copy link
Contributor

The predicate.

This code compiles because you created a local const variable c that is know to be defined. This is the workaround to this issue.

@ahejlsberg ahejlsberg added the Working as Intended The behavior described is the intended behavior; this is not a bug label Feb 7, 2019
@typescript-bot
Copy link
Collaborator

This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

4 participants