【问题标题】:Typescript: extending module in definition file打字稿:在定义文件中扩展模块
【发布时间】:2016-12-12 23:16:36
【问题描述】:

鉴于this definition file,任何人都可以向我确认,由于没有导出命名空间,因此无法扩展导出的Request 接口?如果可能的话,我将不胜感激任何正确方向的提示:)

我在index.d.ts 中尝试了以下操作,但没有成功:

declare module 'koa' {
    namespace Koa {
        export interface Request {
            body: string;
        }
    }
}

谢谢你,罗宾

【问题讨论】:

    标签: typescript typescript1.8


    【解决方案1】:

    可以扩展它,您只需重做整个模块声明即可。

    koa.d.ts:

    declare module 'koa' {
        namespace Koa {
            export interface Request {
                body: string;
            }
        }
    }
    

    someOtherFile.d.ts:

    declare module 'koa' {
        namespace Koa {
            export interface Request {
                head: string;
            }
        }
    }
    

    useIt.ts:

    import {Koa} from 'koa';
    
    let x : Koa.Request;
    
    x.head;  // works just fine
    

    【讨论】:

      猜你喜欢
      • 2018-02-11
      • 2020-05-19
      • 2016-07-04
      • 2019-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-06
      相关资源
      最近更新 更多