0

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;
1
  • Not currently possible Commented Aug 26, 2019 at 12:48

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.