1

I would like to define type that would allow only to use object keys that extends certain type:

interface Foo {
    a: number
    b: string;    
}

const c: TKey<Foo, number> = "a" // ok
const c: TKey<Foo, number> = "b" // nok

I created TKey as such:

type TKey<T extends object, V> = T[keyof T] extends V ? keyof T : never;

however it seems it always unwinds to never - what I'm doing wrong?

playground

Thank you!

pankleks
  • 693
  • 6
  • 14
  • I usually call this type function `KeysMatching`, as shown [here](https://tsplay.dev/WK7L8W). You need to iterate over each key type instead of using `keyof T`. See [the answer to the other question](https://stackoverflow.com/a/54520829/2887218) for more information. – jcalz Dec 15 '21 at 19:41

0 Answers0