microsoft / microsoft/TypeScript

Allow to overwrite generic templates

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

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

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

説明

Search Terms

  • overwrite generic this type
  • overwrite template argument
  • class overwrite generic
  • overwrite type generic

Suggestion

Allow to overwrite generic type templates.

Use Cases

I'm currently developing a modular ORM and query builder, where especially in the query builder part it would be necessary to overwrite a generic template argument.

Examples

Here's a super simple example of a query builder. The select method should return a new instance of Query but with a different subset of T (since it limits the fields being selected).

interface Entity {}

class Query<T extends Entity> {
    findOne(): T;

    select(names: (keyof T)[]): this<Partial<T>>; //doesn't work
}

interface User {
    username: string;
    group: string;
    created: Date;
}

const item: User = new Query<User>().findOne();
const item: Partial<User> = new Query<User>().select(['username']).findOne();

This is just to demonstrate the use case. You could of course further describe the type of select by using Pick etc, but I wanted to keep it simple. Please note that I could return Query<Partial<T>> instead, but that would break class inheritance.

To my awareness this use case can't be covered with the newest TypeScript version.

Another use case I had:

interface Query<T> {
    findOne(): T;
}

interface AdapterInterface {
    createQuery<T extends Entity>(classType: Constructor<T>): Query<T>;
}

class Database<A extends AdapterInterface> {
    constructor (private adapter: A) {}

    // here is the magic with the return type, which doesn't work, but is necessary since
    // `T` of A['createQuery'] needs to be inferred from query<T>.
    query<T extends Entity>(classType: Constructor<T>): ReturnType<A['createQuery']><T> {
        return this.adapter.createQuery(classType);
    }
}

class MyAdapterQuery<T> extends Query<T> {
     additionalMethod(): this;
}

class MyAdapter {
    createQuery<T extends Entity>(classType: Constructor<T>): MyAdapterQuery<T> {
         return new MyAdapterQuery<T>();
    }
}


const database = new Database(new MyAdapter);
database.query(User).additionalMethod();

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 内の generic-template の例から始め、TypeScript が現在、ジェネリッククラスとメソッドの戻り値の型をどのようにモデル化しているかを確認します。意図された型チェックのセマンティクスを特定し、実装箇所とテスト箇所を特定する前に必要なコンパイラ設計を評価します。提案されたケースが、出力される JavaScript を変更せずに型チェックを通過すれば完了です。

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

評価

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

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

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