microsoft / microsoft/TypeScript
Rest parameter in callback function using generic tuple types: Forces definition of all parameters
まだ誰も着手していません。
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.3k
- 平均マージ
- 1日 19時間
- マージ済み PR(30日)
- 117
説明
TypeScript Version: 3.8.0-dev.20191116
Search Terms:
rest parameter, spread, tuple type, generics, conditional types
Code
https://github.com/BTOdell/typescript-tuple-rest-spread-bug
This issue was discovered when implementing a type-safe event emitter class. I've produced a minimum reproducible example at the GitHub link above as well as in a Playground link below.
index.tsdefines an unimplementedEventEmitterthat includes 4 functions which should be equivalent (except for theeventNameparameter).test.tsconsumes theEventEmitterclass and calls the various functions to test type-safety.
An Events interface is used to define the supported events by mapping the event name to a tuple type for the parameters of the event listener.
This interface is passed through a generic parameter E in the Test class and then passed through a generic parameter E in the EventEmitter class.
Expected behavior:
A listener function should be able to define 1 parameter even if the caller might pass 2 parameters. The listener will then only have access to the first parameter passed. This is how JavaScript normally behaves.
Actual behavior:
The unexpected behavior occurs on lines 29 and 33 (in test.ts) and only occurs when the Events interface type has to pass through 2 generic class parameters.
If line 14 is uncommented, and line 13 is commented then all unexpected behavior is resolved.
Error:(29, 47) TS2345: Argument of type '(b: boolean) => void' is not assignable to parameter of type '(...args: Args<E["multiArray"]>) => any'.
Types of parameters 'b' and 'args' are incompatible.
Type 'Args<E["multiArray"]>' is not assignable to type '[boolean]'.
Type 'E["multiArray"] | [E["multiArray"]]' is not assignable to type '[boolean]'.
Type 'any[] & E["multiArray"]' is not assignable to type '[boolean]'.
Types of property 'length' are incompatible.
Type '2' is not assignable to type '1'.
Type '[boolean, boolean]' is not assignable to type '[boolean]'.
Types of property 'length' are incompatible.
Type '2' is not assignable to type '1'.
It seems like the tuple types [boolean, boolean] and [boolean] are not being treated like they're being spread as a rest parameter in a function. Could the context information be getting lost due to jumping through multiple generic parameters?
Playground Link:
Playground link
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず、リンクされた最小再現可能な例で test.ts の 29 行目と 33 行目のエラーを再現し、その後 index.ts の関連する定義を調べてください。2 パラメーターのタプルに対して 1 パラメーターのリスナーが受け入れられ、同時に型安全性が維持されることを確認して、期待される動作を検証してください。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- typescript
- 領域
- compilers
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 35/100