reactjs - Redux createStore<StoreState> 产生错误 : Expected 4 type arguments, 但得到 1

标签 reactjs typescript npm redux react-redux

我正在关注 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/

相关文章:

angular - 将以前的值与输出发射器和 ValueChanges 进行比较

javascript - Angular 2 引导函数给出错误 "Argument type AppComponent is not assignable to parameter type Type"

node.js - 语法错误: Unexpected end of JSON input while parsing near '…: {"name":"@babel/plug' ?

typescript - 在 TypeScript 中导入没有作用域名称的作用域 npm 模块

javascript - 在回调中使用 this.setState

typescript - 在条件类型中使用值而不是类型

reactjs - 如何为 graphql 生成 TypeScript 定义

node.js - 在 Docker : why are they disappearing? 上安装 Node 模块

css - 使用 React Bootstrap react Tsx - 用于事件 NavItem 的内联 CSS

reactjs - 如何在React js应用程序的响应中添加http标题