reactjs - 如何创建动态 typescript 类型 : The expected type comes from property 'type' which is declared here on type 'AppActions'

标签 reactjs typescript redux middleware

我正在使用 Typescript 和 react-redux,并尝试为我的 API 请求制作自定义动态中间件。

这是我的代码:

import { Dispatch } from "react";
import { errorHandler } from "../components/Layout/SnackBar/alert";
import { API } from "../_helpers/api";
import { getTheTime } from "../_helpers/constants";
import { AppActions, AppState } from "../_types";

export const BankPages = {
  posts: { name: "POSTS", api: "/post/v2" },
  lessons: { name: "LESSONS", api: "/lesson/v2" },
  guides: { name: "GUIDES", api: "/lesson/v2/guide" },
  courses: { name: "COURSES", api: "/lesson/v2/course" },
  exercises: { name: "EXERCISES", api: "/lesson/v2?course=125" },
};

export const requestBank = (page: keyof typeof BankPages) => (
  dispatch: Dispatch<AppActions>,
  getState: () => AppState
) => {
  const bankState = getState().bank[page];
  const currentTime = getTheTime();
  const resetTime = currentTime - bankState.nextLoad;
  if (bankState.data && resetTime <= 0) {
    return;
  }
  dispatch({ type: `REQUEST_${BankPages[page].name}` });

  API.get(BankPages[page].api)
    .then((res) =>
      dispatch({
        type: `SUCCESS_${BankPages[page].name}`,
        payload: {
          data: res.data,
          nextLoad: getTheTime(10),
        },
      })
    )
    .catch((err) => errorHandler(err, `FAILURE_${BankPages[page].name}`));
};

我在 type dispatch({ type: ... }) 上收到错误:预期的类型来自此处声明的属性“type”在类型“AppActions”上

这是我的类型:

export const REQUEST_POSTS = "REQUEST_POSTS";

export interface RequestPosts {
  type: typeof REQUEST_POSTS;
}

一切都很完美,我知道这是因为我声明了 typeof REQUEST_POSTS

我该如何修复这个错误?

最佳答案

经过一番搜索,我找到了一个 way使用如下函数在 Typescript 4.1+ 中制作动态字符串:

function makeType<NS extends string, N extends string>(namespace: NS, name: N) {
  return namespace + name as `${NS}${N}`
}
// should be used like this
const sampleType = makeType('REQUEST_', 'POSTS');
// return "REQUEST_POSTS"

然后为了获取字符串作为类型,并从对象生成动态字符串应该像这样使用 as const:

export const BankPages = {
  posts: { name: "POSTS", api: "/post/v2" } ,
  lessons: { name: "LESSONS", api: "/lesson/v2" } ,
  guides: { name: "GUIDES", api: "/lesson/v2/guide" } ,
  courses: { name: "COURSES", api: "/lesson/v2/course" } ,
  exercises: { name: "EXERCISES", api: "/lesson/v2?course=125" },
} as const;

使用这个技巧,当您键入 BankPages.post.name 时,您会看到 "POSTS" 而不是 string

动态调度应该是这样的:

dispatch({ type: makeType("REQUEST_", BankPages[page].name) });

关于reactjs - 如何创建动态 typescript 类型 : The expected type comes from property 'type' which is declared here on type 'AppActions' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65324786/

相关文章:

angular - Angular 2 Observables 和 Http 中的“未定义”

javascript - Node.js 中的 declare var 是什么?

typescript - 如何制作具有全局可访问类型的 NPM 模块

javascript - React-native 和 redux 操作

javascript - 未捕获的类型错误 : Cannot read property 'type' of undefined at start of switch function

reactjs - 如何在隐藏时使用react-spring为react中的div制作动画?

javascript - 在 React.js 中获取两个日期之间的天数差异

javascript - React-navigation重置说明

javascript - 警告 : Failed propType: Required prop `children` was not specified in `Pane` . 检查 `first-time-tab` 的渲染方法

javascript - react-sound-强制React组件更新并定位?