Allow any key type as an index signature parameter type by weswigham · Pull Request #26797 · microsoft/TypeScript (original) (raw)
This allows, eg, symbol
:
interface SymbolMap { [x: symbol]: string; }
interface PropertyMap { [x: string | number | symbol]: string; }
// same as interface PropertyMap { [x: PropertyKey]: string; }
enums:
enum Cardinal { North = "n", South = "s", West = "w", East = "e" } interface CardinalDataMap { [dir: Cardinal]: number; }
enum Places { None, First, Second, Third } interface PlaceMap { [dir: Places]: number; }
literal unions:
interface AdjacencyMap { [side: "top" | "bottom" | "left" | "right"]: number; }
and generics:
interface PartialElementMap { [elem: TElements]: HTMLElement; }
interface SpyProxy { [field: keyof T]: Function; }
as index signature parameter types.
Much of this functionality also exists in mapped types, except they had trouble shuffling around symbol information since there was no kind of index signature a symbol could map to. Unlike mapped types, however, since these are just index signatures, they can be added into any object type and in multiples.
Fixes #26470
Fixes #1863 (caveat: well-known symbols are not currently checked as part of a symbol index signature! Such checking should fall out from removing the concept of well-known symbols and using unique symbols instead)
Fixes #2491 in the original sense (rather than with mapped types).
Related: #28315