镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

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

Description

@Arnavion

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.

Activity

  1. RyanCavanaugh commented on Jan 19, 2016

    @RyanCavanaugh
    Member

    Nathan Shively-Sanders (@sandersn) Daniel Rosenwasser (@DanielRosenwasser) would this be fixed by the proposed contextual typing changes?

  2. DanielRosenwasser commented on Jan 19, 2016

    @DanielRosenwasser
    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 Jason Freeman (@JsonFreeman) and Cyrus Najmabadi (@CyrusNajmabadi) that surfacing the undefined type in error messages is not a problem.

  3. Arnavion commented on Jan 19, 2016

    @Arnavion
    ContributorAuthor

    Even if you didn't, I think I've concluded on several occasions with Jason Freeman (@JsonFreeman) and Cyrus Najmabadi (@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?

  4. DanielRosenwasser commented on Jan 19, 2016

    @DanielRosenwasser
    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. 😄

  5. sandersn commented on Jan 20, 2016

    @sandersn
    Member

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

  6. JsonFreeman commented on Jan 20, 2016

    @JsonFreeman
    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.

  7. DanielRosenwasser commented on Apr 2, 2016

    @DanielRosenwasser
    Member

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

  8. Arnavion commented on Apr 2, 2016

    @Arnavion
    ContributorAuthor

    (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?

  9. JsonFreeman commented on Apr 3, 2016

    @JsonFreeman
    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.

  10. added this to the milestone on Apr 26, 2016
  11. DanielRosenwasser commented on Apr 10, 2019

    @DanielRosenwasser
    Member

    We have empty tuples now, and the new message is

    Property '0' is missing in type '[]' but required in type '[number]'.
    
  12. locked as resolved and limited conversation to collaborators on Oct 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptDomain: Error MessagesThe issue relates to error messagingFixedA PR has been merged for this issueHelp WantedYou can do this

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions