Repository navigation
Bad error message for assigning empty array to tuple type #6524
Description
Activity
RyanCavanaugh commented
on Jan 19, 2016 MemberMore actionsNathan Shively-Sanders (@sandersn) Daniel Rosenwasser (@DanielRosenwasser) would this be fixed by the proposed contextual typing changes?
DanielRosenwasser commented
on Jan 19, 2016 MemberMore actionsIf 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 Jason Freeman (@JsonFreeman) and Cyrus Najmabadi (@CyrusNajmabadi) that surfacing the
undefinedtype in error messages is not a problem.Arnavion commented
on Jan 19, 2016 ContributorAuthorMore actionsEven if you didn't, I think I've concluded on several occasions with Jason Freeman (@JsonFreeman) and Cyrus Najmabadi (@CyrusNajmabadi) that surfacing the
undefinedtype in error messages is not a problem.You don't think it gives the impression of being a compiler bug?
DanielRosenwasser commented
on Jan 19, 2016 MemberMore actionsThat's what I argued at the time because it totally looks like we screwed up somehow. The fact that we're surfacing the
undefinedtype isn't a problem so much as it is that its representation kinda, well, sucks. 😄sandersn commented
on Jan 20, 2016 MemberMore actionsI checked, looks like the error with the change in #6118 is still the same.
JsonFreeman commented
on Jan 20, 2016 ContributorMore actionsIs 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 asundefined[]anyway, so maybe there is no point in having that concept.This is not to say that the type
undefinedshould never be surfaced in an error message. That is a separate discussion. I think the better question is not aboutundefined, 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 commented
on Apr 2, 2016 MemberMore actionsIs this still a bug given that the
nullandundefinedtypes are going to be representable in 2.0?(1.9.0-dev.20160402)
foo.ts:
var x = [];
tsc -d -t es5 foo.tsfoo.d.ts:
declare var x: any[];
foo.ts:
var x: [number] = [];
tsc -t es5 foo.tsfoo.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[]orundefined[]? Should it beundefined[]even if--strictNullChecksisn't passed in?JsonFreeman commented
on Apr 3, 2016 ContributorMore actionsFor 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.- addedHelp WantedYou can do thisYou can do thisDomain: Error MessagesThe issue relates to error messagingThe issue relates to error messaging
on Apr 26, 2016 DanielRosenwasser commented
on Apr 10, 2019 MemberMore actionsWe have empty tuples now, and the new message is
Property '0' is missing in type '[]' but required in type '[number]'.- addedFixedA PR has been merged for this issueA PR has been merged for this issue
on Apr 10, 2019 - locked as resolved and limited conversation to collaborators
on Oct 22, 2025
Reported in the IRC channel against 1.7.5. Still exists in 1.8.0-dev.20160117
I guess
undefinedcomes 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.