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

The error "Object is possibly null or undefined" is ambiguous. #8274

Closed
tinganho opened this issue Apr 24, 2016 · 8 comments · Fixed by #49797
Closed

The error "Object is possibly null or undefined" is ambiguous. #8274

tinganho opened this issue Apr 24, 2016 · 8 comments · Fixed by #49797
Labels
Bug A bug in TypeScript Domain: Error Messages The issue relates to error messaging Good First Issue Well scoped, documented and has the green light Help Wanted You can do this
Milestone

Comments

@tinganho
Copy link
Contributor

I have problem distinguishing if the accessor options or headers or both is possibly undefined or null below.

screen shot 2016-04-24 at 19 22 31

Can it instead of using the text Object is possibly... output the accessor expression 'this.option.headers' is possibly ...?

@mhegazy
Copy link
Contributor

mhegazy commented Apr 24, 2016

we can not write the full expression, this could be a function call, a function expression, a whole program .. the error span seems correct to me.

@sandersn and @DanielRosenwasser any ideas on making this error message better?

@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Apr 25, 2016

You could have a more helpful error message when the possibly null value is an identifier or property/element access, For more complex expressions, just default to the other one.

An alternative is to check for each of the possible forms and report the specifics.

  • Cannot access property '{0}' on a possibly null or undefined value.
  • Cannot access element on a possibly null or undefined value.
  • Cannot call a possibly null or undefined value.
  • Cannot construct a possibly null or undefined value.
  • Any other forms I might not be thinking of.

@mhegazy mhegazy added Help Wanted You can do this Bug A bug in TypeScript labels Apr 25, 2016
@mhegazy mhegazy added this to the Community milestone Apr 25, 2016
@mhegazy mhegazy added the Domain: Error Messages The issue relates to error messaging label Apr 25, 2016
@sandersn
Copy link
Member

If you change the code so that it's options that could be undefined or null, then doesn't the error span shrink to this.options? That seems like it distinguishes well enough between this.options and this.options.headers as the source of the message.

@tinganho
Copy link
Contributor Author

Though it is not every time I just look at my IDE. I some time look at the output of my terminal.

@DanielRosenwasser
Copy link
Member

@tinganho the --pretty compiler option should give some context on what the error span refers to.

I generally agree with @sandersn though, I'd like to see if this is a problem a lot of people run into.

@gertcuykens
Copy link

Not sure if this is related but I have no idea why I get the same error when I try

    const l = document.querySelector('link[rel="import"]') as HTMLLinkElement                                                                    
    if (l == null || l == undefined) return;
    const t = l.import.querySelector('template')

image

@gertcuykens
Copy link

Doh! Sorry I needed to do if (l.import == null ) return;

@fcrespo82
Copy link

Is this issue still open? Can I go for it? Any pointers what file should I go for this fix?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Domain: Error Messages The issue relates to error messaging Good First Issue Well scoped, documented and has the green light Help Wanted You can do this
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants