Omit<keyof typeof SomeObject, "omittedKey"> includes Array prototype keys

Viewed 259

In redux toolkit, I have a slice that is mostly string arrays, and I want an action that pushes a string onto one of the arrays. The action should take the key of the array to push onto, and the string to push.

Here is how I've typed it:

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);
    },
  },
});

When I hover over type ContestFilterKey = keyof ContestFilterKey;, I see that the type is, correctly, a list of the keys of the initialState.

However, when I hover over key: Omit<ContestFilterKey, "entryFee">, I get this:

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

It appears that the latter includes a whole bunch of prototype methods. Of Array? Of Object? Unclear.

I would expect Omit<ContestFilterKey, "entryFee"> to simply be: "gameModes" | "contestTypes" | "entryTypes" | "advancedFilters"

What am I missing here?

1 Answers

Okay, a quick RTFM showed that the utility type I was looking for was not Omit, but Exclude.

Leaving this here for the next dolt who makes this mistake.

Related