microsoft / microsoft/TypeScript
Ensure forward-compatibility of callbacks
まだ誰も着手していません。
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.3k
- 平均マージ
- 2日 4時間
- マージ済み PR(30日)
- 132
説明
Suggestion
🔍 Search Terms
strict callback arguments, too many arguments warning, option
✅ 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
Edit: My original suggestion was unworkable. This is a second attempt.
const nextFrame = new Promise(requestAnimationFrame);
The above results in requestAnimationFrame being called with two arguments rather than the one it accepts. This is fragile, since, in future, a second argument may be added to requestAnimationFrame which makes the above code behave differently.
This problem is worse when it comes to browser-provided functions, as those change with new browser versions, and don't rely on an app redeploy.
This can be worked around using never:
declare function requestAnimationFrame(callback: FrameRequestCallback, _: never): number;
…but it makes for a messy auto-complete, as the 'never' arg shows up.
A solution would be some way to mark a function as "cannot be assigned to a function type with more parameters". This would allow library authors to make this assertion if they want to reserve additional params for future use, and this assertion could be added to DOM functions like requestAnimationFrame.
💻 Use Cases
I wrote a blog post about the risks with this pattern, including a section on how TypeScript doesn't prevent it https://jakearchibald.com/2021/function-callback-risks/
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
requestAnimationFrame の例から始め、リンクされている TypeScript Design Goals と callback-argument の動作を確認します。never-parameter の回避策と提案されている function-type assertion を比較します。既存のランタイム動作を維持し、ライブラリ宣言をどのように作成および検証するかを明確にする、合意済みの設計が必要です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- javascript, typescript
- 領域
- compilers, tooling
- issue の種類
- 機能追加
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 32/100