小编Gor*_*sev的帖子

打字稿:无法导出通用接口模块并包含其他通用接口

我正在尝试为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)

我尝试了几种策略 - 简化示例如下:

1.显而易见的方式

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作为公共属性

2.使用静态接口

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)

generics module export class typescript

9
推荐指数
2
解决办法
5186
查看次数

Typescript没有根据回调返回类型选择正确的重载

给出以下代码(游乐场链接: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.

typescript

6
推荐指数
1
解决办法
447
查看次数

标签 统计

typescript ×2

class ×1

export ×1

generics ×1

module ×1