Skip to content

Cannot destructure a Readonly Tuple #17563

Closed

Description

@SteveALee

TypeScript Version: 2.4.0

Code

// This is OK
type Tuple = [string, number]
const t : Tuple = ['string', 2]
const [s,n] = t

// This errors 
// [ts] Type 'Readonly<[string, number]>' is not an array type.
// const rs: string
type ReadonlyTuple = Readonly<[string, number]>
const rt : ReadonlyTuple = ['string', 2]
const [rs,rn] = rt

Expected behavior:
No errors

Actual behavior:
Error as above

Activity

  1. jcalz commented on Aug 2, 2017

    @jcalz
    Contributor

    This is probably a limitation of the Readonly<> that causes it to lose the "array-ness" of the tuple type. You can work around it as follows:

    type ReadonlyTuple = Readonly<[string, number]> & [string, number];
    const rt: ReadonlyTuple = ['string', 2]
    rt[0] = 'zero'; // error as expected, still read-only
    const [rs,rn] = rt // no error, accepts as array type
  2. olegdunkan commented on Aug 2, 2017

    @olegdunkan

    ReadonlyTuple is mapped type, destructing requires array like type in your case

  3. mhegazy commented on Sep 7, 2017

    @mhegazy
    Contributor

    Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

  4. locked and limited conversation to collaborators on Jun 14, 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

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions