Skip to content

Destructured tuple elements are no longer literals #37427

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

Open
karol-majewski opened this issue Mar 17, 2020 · 0 comments
Open

Destructured tuple elements are no longer literals #37427

karol-majewski opened this issue Mar 17, 2020 · 0 comments
Labels
Bug A bug in TypeScript Help Wanted You can do this
Milestone

Comments

@karol-majewski
Copy link

TypeScript Version: 3.9.0-dev.20200315

Search Terms:

  • tuple
  • destructuring
  • literal types
  • literals
  • inline
  • indexed access

Expected behavior:

When destructuring a tuple of array literals, destructured elements should preserve literal values.

Actual behavior:

Destructured elements are not literals.

Related Issues:

Code

If I create a tuple of literals:

declare function enumerate<T>(): <U extends T[]>(...elements: U) => U;

type Color = 'black' | 'white';

// $ExpectType ["black", "white"] ✅
const colors = enumerate<Color>()('black', 'white');

the accessed elements are also literals; regardless of whether I destructure them inline or read them in separate lines. That's good.

// $ExpectType ["black", "white"] ✅
const [primo, secundo] = colors;

// $ExpectType "black" ✅
const first = colors[0];

// $ExpectType "white" ✅
const second = colors[1];

However, when I do the same with a tuple returned by a function, their “literalness” is preserved only when the values are read using the square bracket notation:

// $ExpectType "black" ✅
const erste = enumerate<Color>()('black', 'white')[0];

// $ExpectType "white" ✅
const zweite = enumerate<Color>()('black', 'white')[1];

but not when the assignment happens via destructuring:

// $ExpectType ["black", "white"], received [Color, Color] ⛔️
const [one, two] = enumerate<Color>()('black', 'white');
Output
"use strict";
// $ExpectType ["black", "white"] ✅
const colors = enumerate()('black', 'white');
// $ExpectType ["black", "white"] ✅
const [primo, secundo] = colors;
// $ExpectType "black" ✅
const first = colors[0];
// $ExpectType "white" ✅
const second = colors[1];
// $ExpectType "black" ✅
const erste = enumerate()('black', 'white')[0];
// $ExpectType "white" ✅
const zweite = enumerate()('black', 'white')[1];
// $ExpectType ["black", "white"], received [Color, Color] ⛔️
const [one, two] = enumerate()('black', 'white');
Compiler Options
{
  "compilerOptions": {
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "strictBindCallApply": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "alwaysStrict": true,
    "esModuleInterop": true,
    "declaration": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "moduleResolution": 2,
    "target": "ES2017",
    "jsx": "React",
    "module": "ESNext"
  }
}

Playground Link: Provided

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Mar 17, 2020
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Mar 17, 2020
@RyanCavanaugh RyanCavanaugh added the Help Wanted You can do this label Mar 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Help Wanted You can do this
Projects
None yet
Development

No branches or pull requests

2 participants