Skip to content

Bad error message for assigning empty array to tuple type #6524

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
Arnavion opened this issue Jan 18, 2016 · 10 comments
Closed

Bad error message for assigning empty array to tuple type #6524

Arnavion opened this issue Jan 18, 2016 · 10 comments
Labels
Bug A bug in TypeScript Domain: Error Messages The issue relates to error messaging Fixed A PR has been merged for this issue Help Wanted You can do this
Milestone

Comments

@Arnavion
Copy link
Contributor

Reported in the IRC channel against 1.7.5. Still exists in 1.8.0-dev.20160117

var x: [number] = [];
foo.ts(1,5): error TS2322: Type 'undefined[]' is not assignable to type '[number]'.
  Property '0' is missing in type 'undefined[]'.

I guess undefined comes from the fact that the RHS array is empty, so its element type is left uninitialized. Maybe you want to detect that and have a different error message? "An empty array is not assignable to type [number]." or something.

@RyanCavanaugh
Copy link
Member

@sandersn @DanielRosenwasser would this be fixed by the proposed contextual typing changes?

@mhegazy mhegazy added the Bug A bug in TypeScript label Jan 19, 2016
@DanielRosenwasser
Copy link
Member

If I understand things correctly, you should get number[] with Nathan's changes.

Even if you didn't, I think I've concluded on several occasions with @JsonFreeman and @CyrusNajmabadi that surfacing the undefined type in error messages is not a problem.

@Arnavion
Copy link
Contributor Author

Even if you didn't, I think I've concluded on several occasions with @JsonFreeman and @CyrusNajmabadi that surfacing the undefined type in error messages is not a problem.

You don't think it gives the impression of being a compiler bug?

@DanielRosenwasser
Copy link
Member

That's what I argued at the time because it totally looks like we screwed up somehow. The fact that we're surfacing the undefined type isn't a problem so much as it is that its representation kinda, well, sucks. 😄

@sandersn
Copy link
Member

I checked, looks like the error with the change in #6118 is still the same.

@JsonFreeman
Copy link
Contributor

Is there such thing as a tuple of length 0? It seems like the error ought to say something like

type [] is not assignable to type [number]

where [] is the tuple of length 0. But I guess a tuple of length 0 is really the same as undefined[] anyway, so maybe there is no point in having that concept.

This is not to say that the type undefined should never be surfaced in an error message. That is a separate discussion. I think the better question is not about undefined, but about how to improve error messages about tuple types. I think that while technically, a tuple type acts as an object with numeric properties, perhaps the error should talk about the "size / length" of the tuple. I think that would be more intuitive for users.

@DanielRosenwasser
Copy link
Member

Is this still a bug given that the null and undefined types are going to be representable in 2.0?

@Arnavion
Copy link
Contributor Author

Arnavion commented Apr 2, 2016

(1.9.0-dev.20160402)

foo.ts:

var x = [];

tsc -d -t es5 foo.ts

foo.d.ts:

declare var x: any[];

foo.ts:

var x: [number] = [];

tsc -t es5 foo.ts

foo.ts(1,5): error TS2322: Type 'undefined[]' is not assignable to type '[number]'.
  Property '0' is missing in type 'undefined[]'.

There's no difference with --strictNullChecks


So is an empty array any[] or undefined[] ? Should it be undefined[] even if --strictNullChecks isn't passed in?

@JsonFreeman
Copy link
Contributor

For the tuple case, it doesn't really matter what the element type is. The array literal is required to have an element at index 0 because of the contextual type, but it does not.

For the other case, if the array did not get widened to any[], I don't think you would be able to subsequently add elements to it, which would be very limiting.

Both of these ideas are independent of strict null checking. That said, I still think it would be reasonable to have tuples of length 0, and then the message would talk about the type [] as the empty tuple type.

@DanielRosenwasser
Copy link
Member

We have empty tuples now, and the new message is

Property '0' is missing in type '[]' but required in type '[number]'.

@DanielRosenwasser DanielRosenwasser added the Fixed A PR has been merged for this issue label Apr 10, 2019
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 Fixed A PR has been merged for this issue Help Wanted You can do this
Projects
None yet
Development

No branches or pull requests

6 participants