【发布时间】:2020-08-12 16:49:54
【问题描述】:
我正在尝试在我的 React 项目中使用 TypeScript
export enum IngridientType {
BreadBottom = "BreadBottom",
BreadTop = "BreadTop",
Meat = "Meat",
Cheese = "Cheese",
Bacon = "Bacon",
Salad = "Salad",
Seeds1 = "Seeds1",
Seeds2 = "Seeds2",
}
export type IngredientsListType<R> = { [key in keyof typeof IngridientType]?: R };
type IBurgerBuilderState<T> = { ingredients: IngredientsListType<T> };
class BurgerBuilder extends Component<{}, IBurgerBuilderState<number>> {
state = {
ingredients: {
[IngridientType.Bacon]: 2,
[IngridientType.Cheese]: 2,
},
};
...
}
但我遇到了一个错误
“BurgerBuilder”类型中的属性“状态”不能分配给相同的 基类型 'Component'。类型'{成分:{[IngridientType.Salad]:数字; [IngridientType.Cheese]:数字; }; }' 不可分配给类型 '只读>'。
我不明白这里到底是什么问题..
【问题讨论】:
标签: reactjs typescript