Skip to content

missleading undefined[] in diagnostic message #7786

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

Closed
zpdDG4gta8XKpMCd opened this issue Apr 2, 2016 · 1 comment
Closed

missleading undefined[] in diagnostic message #7786

zpdDG4gta8XKpMCd opened this issue Apr 2, 2016 · 1 comment
Labels
Duplicate An existing issue was already created

Comments

@zpdDG4gta8XKpMCd
Copy link

image

i suppose that the [] literal doesn't infer undefined as a type of its elements, does it?

@DanielRosenwasser DanielRosenwasser added By Design Deprecated - use "Working as Intended" or "Design Limitation" instead Duplicate An existing issue was already created and removed By Design Deprecated - use "Working as Intended" or "Design Limitation" instead labels Apr 2, 2016
@DanielRosenwasser
Copy link
Member

Actually it does - try out

const x: string = [null];

and you'll see the null type surface itself as well!

Here's what's happening:

  1. We've seen an array literal. We try to get the best common type of its elements.
  2. There are no elements to infer from. We default to the undefined type. Why? Well typically we'll be widened to any[] if it matters anyway.
  3. x has a type annotation. Rather than inferring the widened type of its initializer, we'll just check against string.
  4. Compare undefined[] to string and you get this confusing error message.

We have #6524 to track this issue, but since the null and undefined types are actually going to be representable in 2.0, I'm inclined to say that this is not really bad.

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants