I'm trying to see if I can get VS Code to assist me when typing an object with predefined types.
A dish object can look like this:
{
"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"
}
]
}
My current attempt looks like this
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
}
(not sure if some of them should be interface instead of type)
the plan is to make enums for all "types" of options. The auto-suggestion works fine with the id but not when writing the options.
Working 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>[]
}
