我正在遵循一个TypeScript-React-Starter tutorial,并在src/index.tsx
中创建一个商店。在本教程中,
const store = createStore<StoreState>(enthusiasm, {
enthusiasmLevel: 1,
languageName: 'I\'m fluent in Math and Klingon'
});
会产生错误
Expected 4 type arguments, but got 1.
Issue #136建议使用
import { EnthusiasmAction } from './actions/index';
const store = createStore<StoreState, EnthusiasmAction, any, any>(enthusiasm, {
enthusiasmLevel: 1,
languageName: 'I\'m fluent in Math and Klingon'
});
在其他类似的解决方案中,但这会产生另一个错误:
Argument of type '(state: StoreState, action: EnthusiasmAction) => StoreState' is not assignable to parameter of type 'Reducer<StoreState, EnthusiasmAction>'.
Types of parameters 'state' and 'state' are incompatible.
Type 'StoreState | undefined' is not assignable to type 'StoreState'.
Type 'undefined' is not assignable to type 'StoreState'.
这个问题已经结束了,但从那以后其他人也遇到了同样的问题。
如何创建我的商店?
发布于 2020-03-27 18:44:43
尝试在创建存储时在reducer中创建初始状态。应该能行得通。
export interface IStoreState {
languageName: string;
enthusiasmLevel: number
}
const initState: IStoreState = {
languageName: "TypeScript",
enthusiasmLevel: 0
}
https://stackoverflow.com/questions/56120821
复制相似问题