microsoft / microsoft/TypeScript

Feature request: Override and extend callback argument

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

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

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

説明

🔍 Search Terms

extend callback argument typescript

✅ Viability Checklist
⭐ Suggestion

(Possibly a duplicate, but I couldn't find similar issue)
I often need to change type of a callback argument and it always takes an extra line to perform as assertions. I propose to add ability to use as keyword directly on the argument.

function withCallback(callback: (arg: { foo: string }) => void) {
  // ...
}

withCallback((arg) => {
  const { foo, bar } = arg as { foo: string; bar: string }; // always an extra line
  // ...
});
📃 Motivating Example

It would be nice to have a built-in TS feature that makes possible to do that in a shorter manner:

withCallback(({ foo, bar } as { foo: string; bar: string }) => {
  console.log(foo, bar);
});

// or
withCallback(({ foo, bar } as unknown as { foo: string; bar: string }) => {
  console.log(foo, bar);
});

Overriding the type completely isn't needed too often that's why I propose to also enrich this syntax with extends (or something more applicable) keyword:

withCallback(({ foo, bar } as extends { bar: string }) => {
  console.log(foo, bar);
});

Thinking thru it I think it would make sense to also add as extends for other often appearing use-cases:

const x = { foo: 'foo' };

const y = x as extends { bar: string };
// same as 
const y = x as typeof x & { bar: string };

as extends T at this syntax can be interpreted as as typeof __SELF__ & T

💻 Use Cases

There are million use-cases for this feature. Type assertions in TS are used more often than we might expect while learning it.

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

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

はじめの一歩

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

調査の方向性

この issue では、ソースファイル、テスト、エントリーポイントが指定されていません。まず、提案されているコールバックパラメーターと as extends の構文形式を比較し、設計に関する議論を確認してください。作業が完了するのは、範囲を限定した1つの提案について合意された後です。

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

評価

技術スタック
typescript
領域
compilers
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
説明が足りない
初心者へのやさしさ
25/100

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

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