简体   繁体   English

如何将状态更改操作提取到具有类型参数的命名函数? NgRx,角度

[英]How extract state change action to a named function with typed parameters? NgRx, Angular

Is there a way to create a named state change function, with proper parameter types, that will be accepted in the on method when creating a reducer?有没有办法创建一个命名状态更改函数,具有适当的参数类型,在创建减速器时将在on方法中接受?

I would like to create onLoginSuccessful function that will handle state change and can be passed to the on method in the reducer.我想创建onLoginSuccessful函数,该函数将处理状态更改并可以传递给减速器中的on方法。

But when I tried to create onLoginSuccessful , I am getting TS compilation error.但是当我尝试创建onLoginSuccessful时,出现 TS 编译错误。

//== actions.ts file ==//
export const loginSuccessful = createAction(
    '[Login page] Login successful',
    props<{authToken: string}>()
);

//== reducer.ts file ==//
export const initialState: AuthState = {
    authToken: null
};

// this works
export const reducer = createReducer(
    initialState,
    on(loginSuccessful, (state, action) => {
        return {
            ...state,
            authToken: action.authToken
        };
    })
);

// this does NOT work
// creating named function onLoginSuccess with typed params
function onLoginSuccess(state: AuthState, action: typeof loginSuccessful): AuthState {
    return {
        ...state,
        authToken: action.authToken
    };
}

export const reducer = createReducer(
    initialState,
    on(loginSuccessful, onLoginSuccess) // <-- here on "onLoginSuccess" param throws TS compiler an error
);

TS compilation error: TS编译错误:

Argument of type '(state: AuthState, action: ActionCreator<"[Login page] Login successful", (props: { authToken: string; }) => { authToken: string; } & TypedAction<"[Login page] Login successful">>) => AuthState' is not assignable to parameter of type 'OnReducer<AuthState, [ActionCreator<"[Login page] Login successful", (props: { authToken: string; }) => { authToken: string; '(state: AuthState, action: ActionCreator<"[登录页面] 登录成功", (props: { authToken: string; }) => { authToken: string; } & TypedAction<"[登录页面] 登录成功的参数">>) => AuthState' 不能分配给类型为 'OnReducer<AuthState, [ActionCreator<"[Login page] 登录成功的参数", (props: { authToken: string; }) => { authToken: string; } & TypedAction<"[Login page] Login successful">>]>'. } & TypedAction<"[登录页面]登录成功">>]>'. Types of parameters 'action' and 'action' are incompatible.参数“action”和“action”的类型不兼容。 Type '{ authToken: string;输入 '{ authToken: 字符串; } & TypedAction<"[Login page] Login successful"> & { type: "[Login page] Login successful"; } & TypedAction<"[登录页面] 登录成功"> & { type: "[登录页面] 登录成功"; }' is not assignable to type 'ActionCreator<"[Login page] Login successful", (props: { authToken: string; }) => { authToken: string; }' 不可分配到类型 'ActionCreator<"[登录页面] 登录成功", (props: { authToken: string; }) => { authToken: string; } & TypedAction<"[Login page] Login successful">>'. } & TypedAction<"[登录页面]登录成功">>'. Type '{ authToken: string;输入 '{ authToken: 字符串; } & TypedAction<"[Login page] Login successful"> & { type: "[Login page] Login successful"; } & TypedAction<"[登录页面] 登录成功"> & { type: "[登录页面] 登录成功"; }' is not assignable to type '(props: { authToken: string; }) => { authToken: string; }' 不可分配给类型 '(props: { authToken: string; }) => { authToken: string; } & TypedAction<"[Login page] Login successful">'. } & TypedAction<"[登录页面]登录成功">'。 Type '{ authToken: string;输入 '{ authToken: 字符串; } & TypedAction<"[Login page] Login successful"> & { type: "[Login page] Login successful"; } & TypedAction<"[登录页面] 登录成功"> & { type: "[登录页面] 登录成功"; }' provides no match for the signature '(props: { authToken: string; }): { authToken: string; }' 不匹配签名 '(props: { authToken: string; }): { authToken: string; } & TypedAction<"[Login page] Login successful">'.ts(2345) } & TypedAction<"[登录页面]登录成功">'.ts(2345)

Try using ActionType<typeof loginSuccessful> :尝试使用ActionType<typeof loginSuccessful>

import { ActionType } from "@ngrx/store";

// ...

function onLoginSuccess(state: AuthState, action: ActionType<typeof loginSuccessful>): AuthState {
  return {
    ...state,
    authToken: action.authToken
  };
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM