typescript - Omit<keyof typeof SomeObject, "omittedKey"> 包括数组原型(prototype)键

标签 typescript

在 redux 工具包中,我有一个主要字符串数组的切片,我想要一个将字符串推送到其中一个数组的操作。该操作应该采用要推送的数组的键以及要推送的字符串。

这是我的输入方式:

const initialState = {
  gameModes: [] as string[],
  contestTypes: [] as string[],
  entryFee: {
    max: 10000,
    min: 0,
  },
  entryTypes: [] as string[],
  advancedFilters: [] as string[],
};

type ContestFilterSliceType = typeof initialState;
type ContestFilterKey = keyof ContestFilterKey;

const contestFilterSlice = createSlice({
  name: "constestFilter",
  initialState,
  reducers: {
    addFilter(
      state,
      action: PayloadAction<{ key: Omit<ContestFilterKey, "entryFee">; filter: string }>
    ) {
      const { filter, key } = action.payload;
      state[key].push(filter);
    },
  },
});

当我将鼠标悬停在 type ContestFilterKey = keyof ContestFilterKey; 上时,我看到该类型正确地是 initialState 的键列表。 .

但是,当我将鼠标悬停在 key: Omit<ContestFilterKey, "entryFee"> 上时,我明白了:

key: Pick<"gameModes" | "contestTypes" | "entryFee" | "entryTypes" | "advancedFilters", number | "toString" | "charAt" | "charCodeAt" | "concat" | "indexOf" | "lastIndexOf" | "localeCompare" | ... 40 more ... | "replaceAll">

看起来后者包含了一大堆原型(prototype)方法。数组的?对象?不清楚。

我希望Omit<ContestFilterKey, "entryFee">简单地说:"gameModes" | "contestTypes" | "entryTypes" | "advancedFilters"

我在这里缺少什么?

最佳答案

好的,快速 RTFM 显示我正在寻找的实用程序类型不是 Omit ,但是Exclude .

把这个留给下一个犯这个错误的傻瓜。

关于typescript - Omit<keyof typeof SomeObject, "omittedKey"> 包括数组原型(prototype)键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69958303/

相关文章:

Ionic 2 中的 Typescript 定义文件

javascript - 如何在组合函数中传递泛型,而不是简单类型(例如 : string, 数字)

javascript - 如何在 Typescript 中连接字符串和数字

typescript - 在Vue中将scss变量导入 typescript

javascript - 如何正确地为 Angular 中的表实现 ngx-pagination?

javascript - 让 Webpack 不捆绑文件

typescript - Angular 2 : can't resolve global service

javascript - 如何使用 angular2 更新编辑后的切换

javascript - 带有 Typescript 的 ES6 箭头函数

typescript - 在多个 TypeScript 文件中声明和使用相同的命名空间