Paleo
Paleo

Reputation: 23772

How to remove a parameter when the 'T' generic type is 'void'?

I try to define the type of the resolve callback in the code below.

First try: Just the generic

export interface PromiseToHandle<T> {
  resolve: (result: T) => void  // <----- The question is about that line
  reject: (error: any) => void
  promise: Promise<T>
}

export function promiseToHandle<T = any>(): PromiseToHandle<T> {
  let resolve!: any;
  let reject!: (error: any) => void;
  const promise = new Promise<T>((resolveCb, rejectCb) => {
    resolve = resolveCb;
    reject = rejectCb;
  });
  return { promise, resolve, reject };
}

const pth1 = promiseToHandle<boolean>();
pth1.resolve(true); // OK

const pth2 = promiseToHandle<void>();
pth2.resolve(); // OK

It works the way I need, however the type is incorrect:

const pth3 = promiseToHandle<void>();
const voidResolve: () => void = pth3.resolve // Error: Type '(result: void) => void' is not assignable to type '() => void'.

Second try: With a conditional type

export interface PromiseToHandle<T> {
  resolve: T extends void ? () => void : (result: T) => void
  reject: (error: any) => void
  promise: Promise<T>
}

const pth4 = promiseToHandle<void>();
const voidResolve2: () => void = pth4.resolve // OK

The previous issue is solved. But now the main case doesn't work:

const pth5 = promiseToHandle<boolean>();
pth5.resolve(true); // Error: Argument of type 'true' is not assignable to parameter of type 'false & true'.

I don't know why but when a conditional type is used, then a union (boolean is false | true) is converted to an intersection (false & true)!

Upvotes: 3

Views: 2201

Answers (1)

Titian Cernicova-Dragomir
Titian Cernicova-Dragomir

Reputation: 250366

Your conditional type solution is good, you just need to disable the distribution behavior of conditional types

export interface PromiseToHandle<T> {
  resolve: [T] extends [void] ? () => void : (result: T) => void
  reject: (error: any) => void
  promise: Promise<T>
}

Upvotes: 3

Related Questions