在 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"
我在这里缺少什么?
最佳答案
关于typescript - Omit<keyof typeof SomeObject, "omittedKey"> 包括数组原型(prototype)键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69958303/