Open Anatoly03 opened 1 day ago
Index signatures and mapped types are different features with different (but similar) syntax. Which one are you asking about? If you're writing in
then it's a mapped type.
And does static
really matter much here? Is there a reason why you're focused on static members and not instance members?
🔍 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
.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.
The fun part comes when you have a collection like
Block
orItem
, where you not only want to have some internal state, but also allow to list all "content" statically on the class.