Trying to utilize VS Code for assistance when typing an object with predefined types.
An example of a dish object could be:
{
"id": "dish01",
"title": "SALMON CRUNCH",
"price": 120,
"ingredients": [
"MARINERAD LAX",
"PICKLADE GRÖNSAKER",
"KRISPIG VITLÖK",
"JORDÄRTSKOCKSCHIPS",
"CHILIMAJONNÄS"
],
"options": [
{
"option": "BAS",
"choices": ["RIS", "GRÖNKÅL", "MIX"],
"defaultOption": "MIX"
}
]
}
The current attempt is as follows:
enum DishOptionBaseChoices {
Rice = 'RIS',
Kale = 'GRÖNKÅL',
Mix = 'MIX',
}
type DishOption<T> = {
option: string
choices: T[]
defaultOption: T
}
type DishOptions = {
options: DishOption<DishOptionBaseChoices>[]
}
type Dish = {
id: string
title: string
price: number
ingredients: string[]
options: DishOptions
}
(uncertain if some should be interface
instead of type
)
The objective is to create enums for all "types" of options. The auto-suggestion functions well with the id
, but encounters issues with the options
.
https://i.stack.imgur.com/E3r91.png
Effective solution
type ChoiceForBase = 'RIS' | 'GRÖNKÅL' | 'MIX'
type DishOption<OptionType, ChoiceType> = {
option: OptionType
choices: ChoiceType[]
defaultOption: ChoiceType
}
type Dish = {
id: string
title: string
price: number
ingredients: string[]
options: DishOption<'BAS', ChoiceForBase>[]
}