我正在尝试为Bluebird编写一个CommonJS声明文件,这是一个直接导出通用Promise类的promise库.但是,该库还将几个其他泛型类导出为静态成员(PromiseInspection),并且似乎无法使用typescript对其进行建模.
编辑:用法示例,用于说明模块的导出类的工作方式:
import Promise = require('bluebird');
var promise:Promise<number> = Promise.cast(5);
var x:Promise.PromiseInspection<number> = promise.inspect();
Run Code Online (Sandbox Code Playgroud)
我尝试了几种策略 - 简化示例如下:
declare module "bluebird" {
class PromiseInspection<T> {
// ...
}
class Promise<T> {
PromiseInspection: typeof PromiseInspection; // error
constructor<T>();
inspect():PromiseInspection<T>; // error
static cast<U>(value:U):Promise<U>;
// ...
}
export = Promise;
}
Run Code Online (Sandbox Code Playgroud)
失败,错误无法使用私有类型PromiseInspection
作为公共属性
declare module "bluebird2" {
interface PromiseInspection<T> {
// ...
}
interface Promise<T> {
constructor<T>();
inspect():PromiseInspection<T>;
}
interface PromiseStatic {
new<T>();
PromiseInspection:typeof PromiseInspection;
cast<U>(value:U):Promise<U>; // error
}
export …
Run Code Online (Sandbox Code Playgroud) 给出以下代码(游乐场链接:http://bit.ly/1n7Fcow)
declare function pick<T, V>(f:(x:T, y:V) => V):V;
declare function pick<T, V>(f:(x:T, y:V) => T):T;
var xx = pick((x: number, y:string) => x);
var yy = pick((x: number, y:string) => y);
Run Code Online (Sandbox Code Playgroud)
TypeScript选择了不正确的重载,无法推断出类型xx
.
是否有可能使打字稿选择正确的超载?
注意:为了避免XY问题,这是最初的问题 - http://bit.ly/QXaQGc - 我需要这种重载才能正确建模promises.