TS 中使用 typeof 关键字可以自动获取数据类型
在写 React 项目时,有些时候,你的 state 可能会有默认值,比如:
const initialState = {
loadTest: {
id: '',
editor: '',
},
jsonList: [],
isSetupCheck: false,
}
type IState = typeof initialState
class Comp extends Component<any, IState> {
constructor(props) {
super(props);
this.state = {
...initialState
};
}
}
这样就不用分开定义 state 的初始值和 state 的类型了。