1

I want to write a TypeScript util helper called "KeyByValue" to get a key from object by its value. It is possible?

const keyStorage = {
  blue: 'car',
};

// so 

// type KeyByValue<val> = 
//                                           | value    | key
// I want a TypeScript helper --> KeyByValue<'car'> --> blue
Altaula
  • 774
  • 1
  • 7
  • 22
  • 1
    Sounds like you want `KeysMatching` from [this answer](https://stackoverflow.com/a/55153000/12299000). – kaya3 Apr 10 '22 at 19:27
  • Note that in the example code, the type of `keyStorage.blue` is `string` and not `"car"`. So either you want `KeyByValue` or you want `const keyStorage = {blue: "car"} as const`. Either way it's kind of a too-minimal example since `keyof typeof keyStorage` is just `"blue"` no matter what. – jcalz Apr 10 '22 at 19:31

0 Answers0