microsoft / microsoft/TypeScript

Allow using type parameters and return types with overloaded class constructors

オープン
#35,387 コメント 11 件 リアクション 10 件 担当者 0 名 GitHub で見る

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

Awaiting More Feedback Suggestion
主要言語
Go
スター
111k
フォーク
14.3k
平均マージ
2日 4時間
マージ済み PR(30日)
132

説明

Search Terms

Generic Class Constructor Declaration Overloads Type Parameters

Suggestion

This is partially a request to revisit some of the discussion from here:
https://github.com/microsoft/TypeScript/issues/10860
But it's also to provide some examples of cases that are difficult/impossible to type using classes today.

A class may frequently assign different types to its properties depending on how it was instantiated. While we can type those properties as unions of all their possible types (or even use completely separate classes), it would be amazing if we could "narrow" a class's type based on how it was instantiated.

We CAN handle this type of complexity with regular function overloads, because the relevant call signature is narrowed by both function parameters and type parameters. Class constructors, however, only pay attention to the parameters being passed and can't have type parameters.

Use Cases

// Using type parameters with overloaded generic function
function func(): void;
function func<T extends string>(requiredThing: T): void
function func(requiredThing?: string) { }
func()              // Ok
func<'hello'>()     // Expects 1 argument, as expected

// Attempting to use type parameters with overloaded class constructor
class Example<T> {
    constructor()
    constructor(blah: T)    // How do we connect this to the presence of T?
    constructor(blah?: T) {}
}
new Example()           // Ok
new Example<'hello'>()  // We want this to expect 1 argument somehow

As was also discussed in https://github.com/microsoft/TypeScript/issues/10860, return types on a constructor could theoretically represent narrowed versions of the instance type. Currently there's not an easy way to narrow a property's type based on how the class was instantiated:

class Example {
    prop?: string | number

    constructor()    // When this is used, prop should be string | number | undefined
    constructor(prop: string)    // When this is used, prop should be string
    constructor(prop: number) // When this is used, prop should be number
    constructor(prop?: string | number) {
        // implementation
    }
}

Examples

Ideally, maybe something like this for narrowing by type parameters:

class Example {
    constructor()
    constructor<T extends string>(blah: T)
    constructor(blah?: string) {}
}
new Example()           // Ok
new Example<'hello'>()  // Would expect 1 argument

And maybe something like this for return types:

interface IExampleString extends Example {
    prop: string
}
interface IExampleNumber extends Example {
    prop: number
}
class Example {
    prop?: string | number

    constructor()    // When this is used, prop should be string | number | undefined
    constructor(prop: string): IExampleString    // When this is used, prop should be string
    constructor(prop: number): IExampleNumber    // When this is used, prop should be number
    constructor(prop?: string | number) {
        // implementation
    }
}

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, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

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

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

はじめの一歩

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

調査の方向性

実装ファイル、テスト、エントリポイントは指定されていません。まず issue 10860 にリンクされているディスカッションと、ここにあるコンストラクターオーバーロードの例を確認してください。完了とするには、オーバーロードされたクラスコンストラクターにおける型パラメーターと戻り値の型の絞り込みについて、合意されたセマンティクスと実装サポート、および対応するテストが必要です。

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

評価

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

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

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