Consider a Typescript interface:
interface Product {
url: URL;
available: boolean;
price: number;
}
We need to create a generic type that can produce a builder type based on any given interface:
interface ProductSteps {
url: (data: unknown) => URL;
available: (data: unknown) => boolean;
price: (data: unknown) => number;
}
One approach is to utilize the Record utility type to generate the Builder:
type Steps<T> = Record<keyof T, (data: unknown) => T[keyof T]>
type ProductSteps = Steps<Product>;
Although this method works, it allows any of the value types of Product as return types:
type ProductSteps {
url: (data: unknown) => URL | boolean | number;
available: (data: unknown) => URL | boolean | number;
price: (data: unknown) => URL | boolean | number;
}
Any suggestions on how to constrain the return type of the builder functions to their corresponding type in Product
?