Is there a way in typescript to get the return type of a object property function and use that as the type of another object property function's parameter?
For example:
const request = {
// Infer the return type of this data function as shown to below...
data: () => ({ people: ["bob", "jack"] }),
// The type of `data` parameter should now be an object with people as a prop with the type of string[].
run: (data) => Promise.resolve(data.people),
}
I'm not even sure if this is possible, but the closest I've got is the following...
interface IRequest<TDataProps = {}> {
data: () => TDataProps,
run: <TReturnRun>(data: TDataProps) => MaybePromise<TReturnRun>;
}
// So can manually provide the type but not inferred...
const request: IRequest<{ people: string[] }> = {
data: () => ({ people: ["bob", "jack"] }),
run: (data) => Promise.resolve(data.people),
}
Many thanks