我正在关注 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'.
该问题已关闭,但此后其他人也遇到了同样的问题。
如何创建我的商店?
最佳答案
const store = createStore<StoreState>(enthusiasm, { enthusiasmLevel: 1, languageName: 'I\'m fluent in Math and Klingon' });
produces the error
Expected 4 type arguments, but got 1.
这是由于我使用 Redux 4.0.1 时使用 Redux 3.7.2 的教程。
解决方案 #1
我安装了 Redux 3.7.2:
npm install redux@3.7.2
因为我使用的是 TypeScript-React-Starter tutorial ,这是最适合本教程的解决方案。
解决方案#2
我更改了 createStore() 函数以采用 4 个类型参数作为错误消息建议:
const store = createStore<StoreState, Action<any>, unknown, unknown>(enthusiasm, {
enthusiasmLevel: 1,
languageName: 'I\'m fluent in Math and Klingon',
});
现在我可以继续 tutorial使用 Redux 4.0.1。 :-)
关于reactjs - Redux createStore<StoreState> 产生错误 : Expected 4 type arguments, 但得到 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56120821/