microsoft / microsoft/TypeScript

Feat: infer parameter types for the implementation signature of an overloaded method

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

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

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

説明

Suggestion

implicitly infer parameter types for the implementation signature of an overloaded method
This issue is a complement to #7763, #34319

🔍 Search Terms

TypeScript interface with multiple signatures;
infer params from different signatures;
implementation signature of an overloaded method;

✅ 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

Add an implicit inference to the params or restricted inference in some cases

📃 Motivating Example

Before, you can achieve simple

class Some {
  async request(action: 'auth', payload: AuthPayload): Promise<AuthResponse>
  async request(action: 'pub', payload: PubPayload): Promise<PubResponse>
  async request<S extends Values<SignalingRequests>>(
    ...params: Parameters<S>
  ): Promise<ReturnType<S>> {
    const [action, payload] = params
    switch (action) { /* ... */ }
  }
}
// redundant declarations
// declare Values<T> = T[keyof T]
declare type SignalingRequests = {
  auth: (action: 'auth', payload: AuthPayload) => AuthResponse
  pub: (action: 'pub', payload: PubPayload) => PubResponse
  // ...
}

Now you can use

class Some {
  async request(action: 'auth', payload: AuthPayload): Promise<AuthResponse>
  async request(action: 'pub', payload: PubPayload): Promise<PubResponse>
  async request(...params) {
    if (params[0] == 'auth') {
      const p: AuthPayload = params[1] // this works
    }
    // or you can write:
    const [action, payload] = params
    switch (action) {
      case 'auth':
        payload // AuthPayload
        break
      case 'pub':
        break
      default:
        // exhaustive check
        const nope: never = action
        throw 'nope'
    }
  }
}

💻 Use Cases

Prevent complex type annotation workarounds, simplify codes, make TypeScript sound again!

Best regards.

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

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

はじめの一歩

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

調査の方向性

この issue にある、動機となるオーバーロードされたメソッドの例から始め、関連する issue #7763 と #34319 を読んでください。実装パラメーターに対する意図された推論規則と、オーバーロード間で narrowing がどのように動作すべきかを明らかにしてください。実装とテストの指針となるのに十分なほど動作が明確に仕様化されていれば完了です。

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

評価

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

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

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