当“ T”通用类型为“ void”时,如何删除参数?

时间:2019-05-01 11:39:10

标签: typescript generics void conditional-types

我尝试在下面的代码中定义resolve回调的类型。

第一次尝试:只是通用

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

它按照我需要的方式工作,但是类型不正确:

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

第二次尝试:使用条件类型

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

先前的问题已解决。但是现在主要情况不起作用:

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

我不知道为什么,但是当使用条件类型时,联合(booleanfalse | true)将转换为交集(false & true)!

1 个答案:

答案 0 :(得分:1)

您的条件类型解决方案很好,您只需禁用条件类型的分布行为

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