type TFruit = 'apple' | 'banana'
type TFruitProps = 'color' | 'size'
const food = {
apple: {
color: 'red'
}
};
food
= любое значение типа TFruit
TFruitProps
Record
type TFood = {
[key in TFruitProps]: string
}
const food:Record<TFruit, TFood> = {
apple: {
color: 'red'
}
}
banana
, хотя я указал его через разделитель |
. Как сделать его необязательным?type TFruit = 'apple' | 'banana'
type TFruitProps = 'color' | 'size'
type Food = {
[key in TFruit]?: {
[key in TFruitProps]?: string;
};
}
const redApple: Food = {
apple: {
color: "red"
}
};
const bigApple: Food = {
apple: {
size: "big",
}
};
const redBigApple: Food = {
apple: {
size: "big",
color: "red"
}
};
const redBanana: Food = {
banana: {
color: "red"
}
};
const bigBanana: Food = {
banana: {
size: "big",
}
};
const redBigBanana: Food = {
banana: {
size: "big",
color: "red"
}
};
const redBigGomunkul: Food = {
apple: {
size: "big",
color: "red"
},
banana: {
size: "big",
color: "red"
}
}
const wtf: Food = {}