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

Directly setting an object which contains an optional item defined with an interface doesn't bypass Object is possibly 'null' or 'undefined'. #37240

Open
mckaysalisbury opened this issue Mar 6, 2020 · 0 comments
Labels
Bug A bug in TypeScript
Milestone

Comments

@mckaysalisbury
Copy link

TypeScript Version: 3.9.0-dev.20200305

Search Terms:

  • object is possibly 'null' or 'undefined'

Code
https://github.com/mckaysalisbury/typescript-direct-set

// See the above repo for a little more self containment.
// Most notably `"strictNullChecks": true`
interface I {
  optional?: (I | null);
  required: number;
}

let workaround: I;
workaround = {
  optional: { required: 2 },
  required: 1
};

workaround.optional.required;  // This is where the error occurs

Expected behavior:
This code should properly compile without errors, even with "strictNullChecks": true
Actual behavior:
With "strictNullChecks": true, there is a possibly undefined error.

Playground Link:
Typescript Playground

Related Issues:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript
Projects
None yet
Development

No branches or pull requests

2 participants