For this specific question, the focus is on typings.
Imagine we have a basic generic function type:
type Fn<T> = (input: T) => boolean
The goal is to create a function that, when given an object type as a parameter, will accept an object with the same structure as the generic argument (same keys). However, the values should be of type Fn
, where the fields' types match the generic argument.
Here's an example:
type Val = {
a: string
b: number
}
If we pass this type Val
as a generic argument to function f
, it should receive an object like this:
f<Val>({
a: (input: string) => true, // Fn<string>
b: (input: number) => false, // Fn<number>
c: (input: boolean) => false // Compiler error
// a: (input: number) => false -> would also be a compile error
})
How can we define the type of the function's argument here? Is it achievable in TypeScript?
function f<T>(input: ???): void {
//...
}