Allow a type annotation for indicating the type of a destructured rest assignment

オープン
#45,516 コメント 1 件 リアクション 7 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
5/5
見積もり時間
1週間以上
初心者へのやさしさ
30/100
issue の種類
機能追加
明瞭さ
おおむね明確
活発さ
停滞
技術スタック
javascript, typescript
領域
compilers

調査の方向性

ファイル、テスト、エントリーポイントは指定されていません。まず、提案されている destructuring-rest アノテーション構文と、それを動機付ける例を確認してください。意図した型チェックの動作について合意され、コンパイラーテストでカバーされれば完了です。

索引モデルが issue の本文から書いたものです。

説明

Awaiting More Feedback Suggestion

Suggestion

I offer allow to assign type to rest in destructuring assignment.

🔍 Search Terms

destructuring assignment, desctructuring, rest

✅ Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

const { e, ...rest }: { e: number, ...rest: ICalculatedNumbers } = test;

For better understanding please check Use Cases

📃 Motivating Example

I wanted to assign new calculated value to destructured assigment to field that moved out to new variable by destructuring, but I can't do it because new type exists without field and I'm not able to assign new type to desctructuring assignment.

Provided example in Use Cases

💻 Use Cases

Current behavior in TypeScript
export interface INumbersE {
    c: number;
    d: number;
}

export interface INumbers {
    a?: number;
    b?: number;
    c?: number;
    d?: number;
    e?: INumbersE;
}

export interface ICalculatedNumbers {
    a?: number;
    b?: number;
    c?: number;
    d?: number;
    e?: number;
}

const calcTheNumber = (numbers: ICalculatedNumbers) => {
    // another logic goes there
    console.log(numbers);
}

const test: INumbers = {
    a: 1,
    b: 2,
    c: 3,
    d: 4,
    e: {
        c: 2,
        d: 3
    }
};


const { e, ...rest }: { e: number, ...rest: ICalculatedNumbers } = test; // Property 'rest' is missing in type 'INumbers' but required in type '{ e: number; rest: ICalculatedNumbers; }'

if (e && e.c + e.d === 5) { // Property 'd' does not exist on type 'number' || Property 'c' does not exist on type 'number'
    rest.e = 5; // Property 'e' does not exist on type '{ a: number; b: number; c: number; d: number; }'
}

calcTheNumber(rest); // Type '{ rest: ICalculatedNumbers; }' has no properties in common with type 'ICalculatedNumbers'.
Running in pure JS right as expected behavior and getting expected object with e: 5
const test = {
    a: 1,
    b: 2,
    c: 3,
    d: 4,
    e: {
        c: 2,
        d: 3
    }
};

const calcTheNumber = (numbers) => {
    console.log(numbers); // { a: 1, b: 2, c: 3, d: 4, e: 5 }
}

const { e, ...rest } = test;

if (e && e.c + e.d === 5) {
    rest.e = 5;
}

calcTheNumber(rest);

If cover errors with //@ts-ignore, it'll run as expected, but I'm not ok with that.

主要言語
Go
スター
111k
フォーク
14.4k
平均マージ
1日 19時間
マージ済み PR(30日)
117

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

microsoft/TypeScript のほかの issue

microsoft/TypeScript の issue をすべて見る

似ている issue

Go の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。