'Typescript Type for KeysOfType [duplicate]

I am looking for a typescript type that can handle keys of a certain type. The current solutions that I was able to find reject keys that have multiple types.

interface Demo {
  foo: string | number;
  bar: number;
  name: string;
}

type keys = KeysOfType<Demo, number>;
// keys = "foo", "bar"

How would I declare KeysOfType to work this way?

This is what I have so far

type KeysOfType<T, V> = {
  [K in keyof T]: T[K] extends V ? K : never;
}[keyof T];


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source