microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
100.15k stars 12.38k forks source link

Compile error if I named last array destructuring element. #41548

Open heroboy opened 3 years ago

heroboy commented 3 years ago

TypeScript Version: v4.0.5

Search Terms: destructuring assignment , Object literal may only specify known properties

Code

declare function foo<T extends { dataType: 'a' | 'b'; }>(template: T): [T, any, any]; //<-- there no error, when it is [any,any,any]
declare function bar<T extends { dataType: 'a' | 'b'; }>(template: T): [T, any];

function test_foo() {

  const [, ,] = foo({ dataType: 'a', day: 0 });
  const [x, y, z] = foo({ dataType: 'a', day: 0 });
  const [, , t] = foo({ dataType: 'a', day: 0 }); //<-- red line under the `day`
  console.log(x, y, z, t);
}

function test_bar() {

  const [,] = bar({ dataType: 'a', day: 0 });
  const [x, y] = bar({ dataType: 'a', day: 0 });
  const [, z] = bar({ dataType: 'a', day: 0 });//<-- red line under the `day`
  console.log(x, y, z);
}

Expected behavior: no compile error Actual behavior:

    Argument of type '{ dataType: "a"; day: number; }' is not assignable to parameter of type '{ dataType: "a" | "b"; }'. Object literal may only specify known properties, and 'day' does not exist in type '{ dataType: "a" | "b"; }'.

**Playground Link: link

Related Issues:

theGreen233 commented 3 years ago

Hello, we are a group of researchers developing machine learning techniques to locate issues suitable for newcomers, and our model consider this issue as likely a "good first issue". May we recommend you to label it as "good first issue" so newcomers know where to choose?

Thank you!

andrewbranch commented 3 years ago

This is not a good first issue

typescript-bot commented 2 years ago

:wave: Hi, I'm the Repro bot. I can help narrow down and track compiler bugs across releases! This comment reflects the current state of the repro in the issue body running against the nightly TypeScript.


Issue body code block by @heroboy

:x: Failed: -

Historical Information
Version Reproduction Outputs
4.2.2, 4.3.2, 4.4.2, 4.5.2, 4.6.2

:x: Failed: -

  • Argument of type '{ dataType: "a"; day: number; }' is not assignable to parameter of type '{ dataType: "a" | "b"; }'. Object literal may only specify known properties, and 'day' does not exist in type '{ dataType: "a" | "b"; }'.
  • Argument of type '{ dataType: "a"; day: number; }' is not assignable to parameter of type '{ dataType: "a" | "b"; }'. Object literal may only specify known properties, and 'day' does not exist in type '{ dataType: "a" | "b"; }'.