I'm grappling with the challenge of creating a type that can utilize the typeof its own keys, but I'm hitting a roadblock. Below is a simplified version of what I'm attempting to achieve:
type SpecialType = Record<string, (getter: <K extends keyof SpecialType>(key: K) => ReturnType<SpecialType[K]>) => any>
const yo: SpecialType = {
a: get => {
get('b') // okay, should return string type
return 1 // returns number
},
b: get => {
get('a') // okay, should return number type
get('c') // should throw an error
return 'a' // returns string
}
}
The defined type does not work as expected. My goal is for the parameter get
in each function to be limited to the keys of the parent object, and ideally determine the return type of that function based on the key used in get
.
My current approach looks something like this;
type SpecialType<T extends Record<string, (getter: <K extends keyof T>(key: K) => ReturnType<T[K]>) => any>> = T
However, this doesn't function without the user explicitly specifying the generic themselves (which I am trying to avoid).