microsoft / microsoft/TypeScript

Reusing function type with different parameters amount (overloading)

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

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

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

説明

Bug Report

Is there any reason it's not possible to re-use a function type that is overloaded aka has different possible amount of arguments?

In the playground I put some code that shows this issue, but I will put a quick snippet here:

declare function foo(c: {a: number; b: string}): void
declare function foo(a: number, b: string): void


foo({ a: 1, b: "2" })

foo(1, "2")

// This doesn't work but is relatively understandable
const bar1: typeof foo = (a_c, b) => {
    a_c
    b
}

// This should work, why it doesn't?
const bar2: typeof foo = (...args) => {
    args
}

// it works fine, but you can't access the second parameter
const bar3: typeof foo = (a_c) => {  
    a_c
}

// it works fine, but you can't access the parameters
const bar4: typeof foo = () => {
}

bar2 in this example doesn't work, and the error that is given is Type '(args_0: number | { a: number; b: string; }, args_1: unknown) => void' is not assignable to type '{ (c: { a: number; b: string; }): void; (a: number, b: string): void; }'.

🔎 Search Terms

arguments overload parameter function

🕗 Version & Regression Information

In 4.4.0-beta & 4.3.5:

image

In 4.2.3 and before:

image

⏯ Playground Link

https://www.typescriptlang.org/play?target=99&jsx=0&ts=4.3.5#code/CYUwxgNghgTiAEAzArgOzAFwJYHtVJxwAowAueAbynNWQFsAjEGAbngfIGcMYtUBzAL4BKcgDccWYAFgAUKEiwEKdNjwFi1eLUbMANOy48+-UfAlS5V2YkJEK8LQEYDHeACIATO-gjrt4hcPb2FrAHow+AAVAAssTnhgHBBOVAByDHgAdxwYAGt2ZEz4+DhobDEQCABPeDRQGG4oVGAoBggQOTA8bnZYJ3IMaoAHEBxEDXgAXngiKAB9MFdhaYA+Sjl4LcdFze2GOUFwyNiSzhicZAhgbNy8gyyY2qxMpJT0jAB+Lp7MhlhPIMRmMJgFprMAHRQ2D8TgrKbrCh7LYwziHY7wF63fIJRB8ECuIrwaqXeBgZoZRxgMApBIYGIITjgPA3YawKB0EAYZg-VC9f4wADMQNG40mMzmi3hiK2yJ2YHRsjkEUxmRyOKQ+MJmRJyDJFMyUGptPg9IQbJgHK5zDRsm6fL+sAALCKQeLZtKNrIjrIgA

🙁 Actual behavior

It just errors out when trying to use "...args" to get all the possible arguments of the function

🙂 Expected behavior

To be able to use "...args" or similar to get the arguments and typescript to not complain just for trying to use it

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

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

はじめの一歩

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

調査の方向性

まず、報告されているバージョン 4.3.5 と 4.4.0-beta を使って、リンク先の TypeScript Playground でオーバーロードの代入動作を再現します。受け入れられる例と拒否される例を比較し、その後、オーバーロードされた関数の rest パラメーター実装に対して意図される動作を判断します。動作と期待される型付けが確立され、適切な compiler test でカバーされれば完了です。

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

評価

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

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

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