microsoft / microsoft/TypeScript

Static Index Signature with Generics

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

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

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

説明

🔍 Search Terms
✅ Viability Checklist
⭐ Suggestion

Currently, static index signatures can be declared for a type, under the condition, that the mapping cannot be a literal type or generic type. My proposal is to allow TypeScripts' powerful typing system on static indexing by removing this restriction. As you see below, there is one utility type that already allows the elimination of a string subset, that being Uppercase.

class Abc{
    static readonly [k: number]: number;
}

class Def{
    static readonly [k: number | Uppercase<string>]: number; // This is allowed
    static hello() {} // Since `'hello' extends Uppercase<string> ? true : false` yields false, this is allowed.
}

By allowing generic indexing, the "value" type can be further specified by the "key" type. Currently, static index signatures with string mapping have the problem of overriding (lowercase) method types, causing the type checker to warn a conflict. By allowing TS' conventional T in Types notation, the subset of index keys can be decreased and shown in InteliSense, and also providing the value type based on key set.

📃 Motivating Example

TypeScript's powerful type system can now be used in static and non-static index signatures, allowing you to type generic indexation.

💻 Use Cases

This can be used to provide great simplifications for API writers, by exposing a niche interface and type hinting on a class. Currently this is possible by obfuscating a class with another type, which is not that great, or using a typed proxy, which is the best approach currently. However all of these mean that the projects' classes have to be split and harder to maintain. There is no way, other than with Uppercase to limit static string index signatures.

By using generic index signatures, as well as being able to use them in static context, a lot of type workload can be exercised within a class range.

const Format = {
    'PlayerJoin': [number, string],
    'PlayerChat': [number, string],
    'PlayerLeave': [number],
}

export class MessageType<F extends keyof typeof Format> {
    // This will allow to index `MessageTypes` as a collection of data entries.
    [Index in number]: (typeof Format)[F][Index];

    // Type System: (typeof Format)[F][0] ~> [number, ...][0] ~> number
    public get playerId() { return this[0]; }

    // This is already possible, nothing of too much interest
    constructor(messageType: F) { return Proxy(...) }
}

The fun part comes when you have a collection like Block or Item, where you not only want to have some internal state, but also allow to list all "content" statically on the class.

const Blocks = [
    { id: 0, name: "air" },
    { id: 1, name: "earth" },
    { id: 2, name: "water" },
];

export class Block<Id extends (typeof Blocks)[number]['id']> {
    // Currently, InteliSense will tell you, that using the generic argument from the class is not supported. This should be kept as a feature, but adding new generic types should not be hindered
    static readonly [SId in (typeof Blocks)[number]['id']]: Block<SId>;
    ...
}

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

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

はじめの一歩

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

調査の方向性

この issue では実装ファイル、テスト、エントリーポイントが指定されていないため、まず提案されている静的ジェネリックインデックスシグネチャとリンクされた issue を確認します。型システムの動作と互換性ルールを定義し、要求されたジェネリックな静的インデックスアクセスを検証できるよう、受理されるケースと拒否されるケースを網羅するコンパイラテストを追加します。

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

評価

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

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

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