Skip to content

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

Open
@heroboy

Description

@heroboy

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:

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptHas ReproThis issue has compiler-backed repros: https://aka.ms/ts-repros

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions