Skip to content

Tuple destructuring type is different for different target options. #37045

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

Closed
nicu-chiciuc opened this issue Feb 26, 2020 · 1 comment
Closed
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@nicu-chiciuc
Copy link

TypeScript Version: 3.9.0-dev.20200226

Search Terms: target, indexing, tuple, destructuring, array, undefined, es5

Code

const arr: Array<string> = [];

const [first] = arr;
const zero = arr[0];

const str1: string = first;
const str2: string = zero;

also patch lib.es5.d.ts
from

 interface Array<T> {
  //...
  [n: number]: T;
 }

to

 interface Array<T> {
  //...
  [n: number]: T | undefined;
 }

Expected behavior:
Expect tsc to report 2 errors irrelevant of which target version is selected.

Actual behavior:
When using option "target": "es6":

src/index.ts:8:9 - error TS2322: Type 'string | undefined' is not assignable to type 'string'.
  Type 'undefined' is not assignable to type 'string'.

8   const str2: string = zero;
          ~~~~


Found 1 error.

When using option "target": "es5":

src/index.ts:6:7 - error TS2322: Type 'string | undefined' is not assignable to type 'string'.
  Type 'undefined' is not assignable to type 'string'.

6 const str1: string = first;
        ~~~~

src/index.ts:7:7 - error TS2322: Type 'string | undefined' is not assignable to type 'string'.
  Type 'undefined' is not assignable to type 'string'.

7 const str2: string = zero;
        ~~~~


Found 2 errors.

Playground Link: https://github.com/nicu-chiciuc/typescript-issue-001

Related Issues:
#13778
#36635

The whole idea of chaining the indexing definition for arrays comes from the first issue and mentioned this problem in this comment.
I didn't realize that the behavior depends on the version of the target option.

@RyanCavanaugh RyanCavanaugh added Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript labels Feb 26, 2020
@RyanCavanaugh
Copy link
Member

Some level of difference should be expected here since since we know that when targeting ES5 this will be downleveled to arr[0], whereas in a native ES6 runtime this will go through the Iterable protocol and possibly produce different results.

It looks like we have some shortcut paths in the checker that assume an Iterable from Array<T> will produce T when determining the iteration path. We could probably change that to be slower if multiple people need this support.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

2 participants