Seeking input on improving the title of this code snippet. Here's an example that needs your expertise:
type Query = ((...params: any[]) => Promise<any>);
type Resource = {
query: Query;
}
export type Params<T> = T extends ((...params: infer R) => Promise<any>) ? R : any[];
const resource: Resource = {
query: (id: string) => Promise.resolve(id)
}
const inferredResource = {
query: (id: string) => Promise.resolve(id)
}
type ResourceParams = Params<typeof resource['query']>;
type InferredResourceParams = Params<typeof inferredResource['query']>;
The challenge lies in ensuring that the object conforms to the shape defined by Resource
, while also inferring its parameters for later use.
In the current setup, either the structure is guaranteed but the inference fails, or vice versa. Is there a way to achieve both goals simultaneously?