There is a function that retrieves a value by key from an object and provides suggestions of possible keys in the record when using it. This function also infers types from its arguments.
function get<T extends Record<string, any>, K1 extends keyof T>(key1: K1):
(record: T | undefined) => T[K1] | undefined
const foo = { bar: 1, baz: 2 }
get('bar')(foo) // 1, get<{ a: number, b: number }>
Unfortunately, my attempts at creating a pointfree function have been unsuccessful. I have examined the implementation of the prop
function in Ramda library, which works in a pointfree manner but does not provide suggestions as it allows any string as a key.
type prop = <P extends string>(key: P) => <T>(obj: Record<P, T>) => T
const foo = { bar: 1, baz: 2 }
prop('bar')(foo) // 1, prop: <"bar">(key: "bar") => <T>(obj: Record<"bar", T>) => T
EDIT:
I am aware that I cannot retrieve suggestions without specifying the record first.
prop('...') // no suggestions here
prop('...')(foo) // now I want suggestions