Redux的createStore<StoreState>产生错误:预期4个类型参数,但只有1个。

5

我正在跟随TypeScript-React-Starter教程,并在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.


问题 #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'.

问题已经关闭,然而其他人之后也遇到了同样的问题。
如何创建我的商店?
2个回答

2
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 3.7.2,而我使用的是 Redux 4.0.1。


解决方案 #1
我安装了 Redux 3.7.2:
npm install redux@3.7.2

由于我正在使用 TypeScript-React-Starter 教程,因此这是与教程最相符的解决方案。


解决方案 #2
我修改了createStore()函数,按照错误信息建议,将其改为接受4个类型参数。
const store = createStore<StoreState, Action<any>, unknown, unknown>(enthusiasm, {
  enthusiasmLevel: 1,
  languageName: 'I\'m fluent in Math and Klingon',
});

现在我可以继续使用Redux 4.0.1来完成tutorial。:-)

0
尝试在创建存储时在reducer中创建初始状态,这样应该可以正常工作。
export interface IStoreState {
    languageName: string;
    enthusiasmLevel: number
}

const initState: IStoreState = {
    languageName: "TypeScript",
    enthusiasmLevel: 0
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接