When an index signature is not available, encourage using a more specific type to index the type. #36962
Open
5 tasks done
Labels
Awaiting More Feedback
This means we'd like to hear from more people who would be helped by this feature
Suggestion
An idea for TypeScript
Search Terms
index signature, keyof, domain: error messages
Vaguely related:
#14951
Suggestion
When there is a type error because of a missing
string
index signature, add a suggestion of:"Did you mean to use a more specific type such as
keyof Thing
instead ofstring
?"Only show this message:
Use Cases
Take the following example:
The error message here is:
This is technically correct! However, many people read this error message to mean "add an index signature to
Person
," rather than advising them to use a more specific type thanstring
, likekeyof Person
. Adding the index signature suppresses the error, making it seem like the correct solution.Where this doesn't work
There are some cases where this advice would not make sense:
Other cases I can think of so far:
reduce
(suppress for empty objects)Object.keys
andObject.entries
as discussed many, many times (require the indexing type to be in the same scope)Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: