Reputation: 3878
is there a way to define Index types for typescript with a specific pattern? something like all properties starting with "$" are HTMLElements. something like
[key: "^\$.+"]: HTMLElement;
Upvotes: 0
Views: 105
Reputation: 6994
No, this is currently not possible, index signatures are limited to the following:
There are two types of supported index signatures: string and number. It is possible to support both types of indexers, but the type returned from a numeric indexer must be a subtype of the type returned from the string indexer. This is because when indexing with a number, JavaScript will actually convert that to a string before indexing into an object.
There are some proposals for allowing exactly what you described, but no significant effort for an implementation has happend yet.
Upvotes: 1