相关疑难解决方法(0)

覆盖Typescript d.ts文件中定义的覆盖接口属性类型

有没有办法更改*.d.tsintype中定义的接口属性的类型?

例如:接口in x.d.ts定义为

interface A {
  property: number;
}
Run Code Online (Sandbox Code Playgroud)

我想在我写的typescript文件中更改它

interface A {
  property: Object;
}
Run Code Online (Sandbox Code Playgroud)

甚至这会起作用

interface B extends A {
  property: Object;
}
Run Code Online (Sandbox Code Playgroud)

这种方法会起作用吗?当我尝试使用我的系统时,它无法正常工作.只是想确认它是否可能?

javascript typescript typescript-typings

60
推荐指数
14
解决办法
6万
查看次数

如何在定义为"any"的第三方TypeScript接口中扩充属性?

给定第三方TypeScript模块,如下所示:

// in /node_modules/third-party-module/index.d.ts
declare module 'ThirdPartyModule' {
    export interface ThirdPartyInterface {
        data: any;
    }
}
Run Code Online (Sandbox Code Playgroud)

如何扩充此模块以更严格地键入data属性?

我试过这个:

// in /app/typings.d.ts
declare module 'ThirdPartyModule' {

    interface IMyCustomType {}

    interface ThirdPartyInterface {

        // causes a compiler error: Subsequent property declarations 
        // must have the same type.  Property 'data' must be of type 
        // 'any', but here has type 'IMyCustomType'.
        data: IMyCustomType;
    }
}
Run Code Online (Sandbox Code Playgroud)

但这给了我一个编译器错误:"后续属性声明必须具有相同的类型.属性'数据'必须是'any'类型,但这里的类型为'IMyCustomType'."

如果第三方模块将属性定义为实际类型,如下所示:

// in /node_modules/third-party-module/index.d.ts
declare module 'ThirdPartyModule' {
    interface IAnotherThirdPartyInterface {
        something: string;
    }

    interface …
Run Code Online (Sandbox Code Playgroud)

module interface typescript

8
推荐指数
1
解决办法
1773
查看次数