microsoft / microsoft/TypeScript
Static Index Signature with Generics
Personne n'a encore pris cette issue.
- Langage dominant
- Go
- Étoiles
- 111k
- Forks
- 14.3k
- Merge moyen
- 2 j 4 h
- PR mergées (30 j)
- 132
Description
🔍 Search Terms
- static mappings with generics
- static generic index signature
- class index signature static
- generic index signature
- https://www.typescriptlang.org/docs/handbook/2/classes.html#:~:text=%5Bs%3A%20string%5D%3A%20boolean%20%7C%20((s%3A%20string)%20%3D%3E%20boolean)%3B
✅ Viability Checklist
- 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 isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
⭐ 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>;
...
}
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
L’issue ne nomme aucun fichier d’implémentation, test ou point d’entrée ; commencez par examiner les signatures d’index génériques statiques proposées et les issues associées. Définissez le comportement du système de types et les règles de compatibilité, puis ajoutez des tests du compilateur couvrant les cas acceptés et rejetés afin de vérifier l’indexation statique générique demandée.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- typescript
- Domaine
- compilers
- Type d'issue
- Fonctionnalité
- Difficulté
- 5/5
- Temps estimé
- Plus d'une semaine
- Activité
- À l'abandon
- Clarté
- Plutôt claire
- Accessibilité débutants
- 30/100