Promise的模块扩充

时间:2019-06-04 16:59:54

标签: typescript typescript-typings

我希望有一个通用类型,而不是通用类型,也就是更高种类的类型:

abstract class MyClass<URI> { // URI can be Observable or Promise
  // This obviously doesn't work, but that's the idea I have
  abstract run(): URI<number> {
    //... should return Promise<Number> or Observable<Number>, depending on URI
  }
}

所以我正在尝试描述herehere的方法,基本上是:

export interface HKT<URI, A> {
  readonly _URI: URI;
  readonly _A: A;
}

abstract class MyClass<URI> {
  abstract run(): HKT<URI, number>;
}

我基本上只需要像这样对Observable和Promise进行模块扩充:

declare module "rxjs" {
  interface Observable<T> {
    _URI: "Observable";
    _A: T;
  }
}

但是,如何为Promise进行模块扩充?

// This doesn't work...
interface Promise<T> {
  _URI: "Promise";
  _A: T;
}

这是一个codeandbox链接:https://codesandbox.io/embed/billowing-darkness-89mry

1 个答案:

答案 0 :(得分:0)

找到我的答案。

declare global {
  interface Promise<T> {
    _URI: 'Promise';
    _A: T;
  }
}