Array with option allof in definition to tell TS that all options must be implemented from that array #56273
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
Typescript Define Array type with variations
typescript array mandatory
typescript array with predefined options
✅ Viability Checklist
⭐ Suggestion
Typescript should be extended by a list of available options which a type must implement mandatory
here in my suggestion the keyword
allof
would describe that this array need to implement all options ofLanguageLocale
.allof
should be only usable together with[]
or in case definition is split it should be ignored (e.g. LanguageEntry is used directly somewhere)It should work as well for type
Array<T>
📃 Motivating Example
The motivation is to find places in the code where options are missed but mandatory
Imaging a Language Picker and you added a new language.
In the code the following was present.
It have had following data:
so after adding
"nb-NO"
type is still valid and for now there is now simple solution to make
nb-NO
mandatory to implement💻 Use Cases
It would it make easy to turn runtime errors ins transpile time errors by TS since this could be found before commiting.
There is a workaround but you need to repeat yourfself and have mulitple definitions. So in the end it's just moving the error away from the place in code to the type definition
The text was updated successfully, but these errors were encountered: