1

I'm trying to create types for the library rubico, for fun. I'm in problems to do that because of your generality. For example, the function fork:

type GenericFunction<OUT = any> = (...args: readonly any[]) => OUT;

type Dict<T> = { [k: string]: T };

// https://stackoverflow.com/questions/48011353/how-to-unwrap-type-of-a-promise#fromHistory
type Awaited<T> = T extends PromiseLike<infer U> ? Awaited<U> : T;

declare function fork<
  IN extends Dict<GenericFunction>,
  OUT = { [k in keyof IN]: Awaited<ReturnType<IN[k]>> },
  RS = ReturnType<IN[keyof IN]>,
  >(obj: IN): RS extends Promise<any> ? Promise<OUT> : OUT;

const res1: { x: number, y: string } = fork({
  x: (x: number) => x,
  y: (y: string) => y,
});

// This does't works
const res2: Promise<{ x: number, y: number }> = fork({
  x: (x: number) => x,
  y: async (x: number) => x,
});

// This works
const res3: Promise<{ x: number, y: number }> = fork({
  x: async (x: number) => x,
  y: async (x: number) => x,
});

I'm a bit tired to continue thinking about that alone, so if someone can help me, I will thank you very much.

Edit 1

I want to know if it's possible to force the typescript to use Promise ever when any type of the return of the input object is Promise?

0 Answers0