我有用类型构建的枚举和枚举组.当我使用enum组(在本例中是StoneFruit和CitrusFruit)来输入组件的状态时,我会出现类型脚本错误.但错误消息对我来说绝对没有意义
Type Fruits is not assignable to type StoneFruit
TS2416: Property state in type Fruit is not assignable to the same property in base type Component<any, FruitState, any>
Type { one: Fruits; two: Fruits; } is not assignable to type Readonly<FruitState>
Types of property one are incompatible.
Type Fruits is not assignable to type StoneFruit
import React from "react"
enum Fruits {
Orange,
Lemon,
Peach,
Plum
}
type StoneFruit = Fruits.Peach | Fruits.Plum
type CitrusFruit = Fruits.Orange | Fruits.Lemon
type FruitState = {
one: StoneFruit
two: CitrusFruit
}
class Fruit extends React.Component<any, FruitState> {
state = {
one: Fruits.Peach,
two: Fruits.Orange
}
}
const testState: FruitState = {
one: Fruits.Peach,
two: Fruits.Orange
}
然而,如果我将状态与以下内容一起放置,则不会再有错误.
state : FruitState = {
one: Fruits.Peach,
two: Fruits.Orange
}