我有一个看起来像这样的组件:
export interface Props {
// ... some props
}
export interface State {
readonly mode: "add" | "delete"
}
export class MyComponent extends Component<Props, State> {
readonly state = { mode: "add" }
// ... more component code
}
问题是这会引发 linting 错误:
Property 'state' in type 'ScannerScreen' is not assignable to the same property in base type 'Component<Props, State, any>'.
Type '{ mode: string; }' is not assignable to type 'Readonly<State>'.
Types of property 'mode' are incompatible.
Type 'string' is not assignable to type '"add" | "delete"'.
为什么 TypeScript 不能识别"add"
或者"delete"
是字符串或者"add"
是模式允许的类型之一?