Can this be achieved in Typescript?
I am looking to transform something like the following:
interface IPromiseObject {
promiseA: Promise<number>;
promiseB: Promise<string>;
}
Into this:
interface IResolvedPromiseObject {
promiseA: number;
promiseB: string;
}
I aim to resolve an object with promises as values while maintaining type safety. Specifically, I need to convert IPromiseObject
into an object with the same keys but resolved values, which can then be used as a return type.
export async function resolveAllObject<IPromiseObject>( promiseObject: IPromiseObject ): Promise<???> {
const resolvedPromiseObject = {};
for ( const key in promiseObject ) {
const value = await promiseObject[ key ];
Object.assign( resolvedPromiseObject, { [ key ]: value } );
}
return resolvedPromiseObject;
}