首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Redux createStore<StoreState>生成错误:应为4个类型参数,但实际为1

Redux createStore<StoreState>生成错误:应为4个类型参数,但实际为1
EN

Stack Overflow用户
提问于 2019-05-14 06:52:52
回答 1查看 1.9K关注 0票数 5

我正在遵循一个TypeScript-React-Starter tutorial,并在src/index.tsx中创建一个商店。在本教程中,

代码语言:javascript
复制
const store = createStore<StoreState>(enthusiasm, {
  enthusiasmLevel: 1,
  languageName: 'I\'m fluent in Math and Klingon'
});

会产生错误

代码语言:javascript
复制
Expected 4 type arguments, but got 1.

Issue #136建议使用

代码语言:javascript
复制
import { EnthusiasmAction } from './actions/index';

const store = createStore<StoreState, EnthusiasmAction, any, any>(enthusiasm, {
  enthusiasmLevel: 1,
  languageName: 'I\'m fluent in Math and Klingon'
});

在其他类似的解决方案中,但这会产生另一个错误:

代码语言:javascript
复制
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'.

这个问题已经结束了,但从那以后其他人也遇到了同样的问题。

如何创建我的商店?

EN

回答 1

Stack Overflow用户

发布于 2020-03-27 18:44:43

尝试在创建存储时在reducer中创建初始状态。应该能行得通。

代码语言:javascript
复制
export interface IStoreState {
    languageName: string;
    enthusiasmLevel: number
}
代码语言:javascript
复制
const initState: IStoreState = {
    languageName: "TypeScript",
    enthusiasmLevel: 0
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56120821

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档