Extract fixed properties from interface

17 Views Asked by At

I have an interface that looks something like this:

interface MyItem extends RowItem {
  id: number;
  name: string;
}

I want to write a function that requires one of the parameters to be one of this type keys, which is not difficult.
The difficulty I'm having is this RowItem type that is simply:

interface RowItem {
  [prop: string]: unknown;
}

This messes with the keyof, as basically any string will be considered a valid key.
Circumstances force me to extend RowItem interface as it is for some reason required for a library that I use.
My question is how do I get a type of MyItem, but without this wildcard [prop: string] property, so I can properly use keyof on it?

So far I've tried the following:

type Attempt1 = {
  // Everything seem to extend unknown, so it all turns never.
  [prop in keyof MyItem]: MyItem[prop] extends unknown ? never : MyItem[prop];
}
// Was left with a [prop: number]
type Attempt2 = Omit<MyItem, string>;
// Was left with a [prop: number] and [prop: string]
type Attempt3 = Pick<MyItem, keyof MyItem>;

I'm at my wits end at this point. Maybe something to do with const keyword, but I'm very unfamiliar with it.
If I were to rewrite it using a type without this RowItem and one with it, I would have to rewrite a lot a lot of code, so I'd rather not do that. I'm talking about 3-digit number of such types all across the code.
To remove extends RowItem and put it where needed as MyItem & RowItem is a possibility, but also a lot of code to cover.
Typescript 4.2.4

0

There are 0 best solutions below