microsoft / microsoft/TypeScript

Static Index Signature with Generics

Open
#60,587 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

🔍 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>;
    ...
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue names no implementation files, tests, or entry points; start by reviewing the proposed static generic index signatures and the linked issues. Define the type-system behavior and compatibility rules, then add compiler tests covering accepted and rejected cases so the requested generic static indexing is verified.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.