Redux工具包+打字稿:额外还原剂

1wnzp6jl  于 2022-11-12  发布在  其他
关注(0)|答案(2)|浏览(157)

我正在尝试向react/redux项目中添加打字脚本。以下是我的部分:

import { createSlice, createAsyncThunk, AnyAction, CaseReducer, PayloadAction } from '@reduxjs/toolkit';
export interface RequestStateBase {
    failed: boolean;
    executing: boolean;
    initialLoad: boolean;
    message: string | null;
    errors: any[]
}
const handlePending = (state: RequestStateBase) => {
    state.failed = false;
    state.executing = true;
    state.message = '';
}

const createCharacter = createAsyncThunk(
    'character/new',
    async (model: any) => characterClient.createCharacter(model)
);

const characterSlice = createSlice({
    name: "character",
    initialState: initialState,
    reducers: {
        cleanCharacterData: (state: CharacterState) => {
            //...
        },
        //...
    },
    extraReducers: builder => {
        builder.addCase(createCharacter.pending, handlePending),
        //...
    }
});

在VS代码中一切正常,但在构建过程中出现以下错误:

Line 78:9:  Expected an assignment or function call and instead saw an expression  @typescript-eslint/no-unused-expressions

线78表示builder.addCase(createCharacter.pending, handlePending)
如何解决这个问题?

wljmcqd8

wljmcqd81#

删除该行后面的,-您可能希望写入;
逗号操作符在这里不会造成任何真实的的伤害,但通常会导致意外的行为--而且它似乎真的会激怒JSHint。

cnjp1d6j

cnjp1d6j2#

对于在这里搜索的任何人来说,我需要为builder参数添加一个类型注解,以便获得对切片状态的类型推断。如果没有类型注解,一旦添加extraReducers,切片就有了推断的类型any
例如,在我的const authSlice = createSlice({ ... })中,extraReducers字段现在被键入为:extraReducers: (builder: ActionReducerMapBuilder<AuthState>) => { ... }

相关问题