Skip to content

Assigning values from tuple. #9874

Closed
Closed

Description

TypeScript Version: 1.8.10

Code

playground link

const someTuple: [string, number] = ['someString', 1];

const first: string = someTuple.shift();

Expected behavior:

TypeScript lets me assign the result of shift() to first: string, or assign the result of pop() to first: number.

Actual behavior:

error TS2322: Type 'string | number' is not assignable to type 'string'.

If I hint first as any, or access someTuple[0], both of those work.

Activity

  1. added
    Too ComplexAn issue which adding support for may be too complex for the value it adds
    Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this feature
    Working as IntendedThe behavior described is the intended behavior; this is not a bug
    Won't FixThe severity and priority of this issue do not warrant the time or complexity needed to fix it
    and removed
    Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this feature
    Too ComplexAn issue which adding support for may be too complex for the value it adds
    Working as IntendedThe behavior described is the intended behavior; this is not a bug
    on Jul 21, 2016
  2. yulianovdey commented on Jul 21, 2016

    @yulianovdey
    Author

    I guess the problem is I'm not supposed to be modifying my "immutable" tuple ;).

  3. DanielRosenwasser commented on Jul 21, 2016

    @DanielRosenwasser
    Member

    The type system doesn't have a way to track the relationship between the first numeric property of a tuple and the behavior of shift (especially when shift changes the contents of the containing array), and I don't think doing so is very realistic.

  4. yulianovdey commented on Jul 21, 2016

    @yulianovdey
    Author

    Daniel Rosenwasser (@DanielRosenwasser) understood, thanks. What about making slice work, since it copies and you could infer types based on each resulting index?

  5. locked and limited conversation to collaborators on Jun 19, 2018
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

    Too ComplexAn issue which adding support for may be too complex for the value it addsWon't FixThe severity and priority of this issue do not warrant the time or complexity needed to fix it

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions