我有一个NPM软件包,其对象如下:
const Lipsum = function constructor(argOne, argTwo) {
...
}
Object.assign(Lipsum.prototype, {
firstMethod: function firstMethod(firstArg, secondArg){...}
secondMethod: function secondMethod(){}
})
module.exports = Lipsum;
我想为其声明一个环境TypeScript类。我在项目shared/lipsum.d.ts
中创建了一个文件,并编写了以下内容:
declare class Lipsum {
constructor(argOne: string, argTwo: number)
firstMethod(firstArg: string, secondArg: string): string
secondMethod(): void
}
export default Lipsum;
我面临以下问题:
const x = new Lipsum(); // <--- this doesn't throw an error, even though the constructor must have two arguments.
x. // <--- It doesn't autocomplete here, but it autocompletes above when I put a dot after "Lipsum()".
更新:
我试图用模块声明包装类,就像这样:
declare module 'lipsum'{
export default class Lipsum {
constructor(argOne: string, argTwo: number)
firstMethod(firstArg: string, secondArg: string): string
secondMethod(): void
}
}
它一直告诉我:
扩充中无效的模块名称。模块'lipsum'解析为'/Users/yaharga/project/node_modules/lipsum/lipsum.js'上的无类型模块,无法进行扩充。
我应该注意代码已经在文件“ lipsum.d.ts”中。
证明所有导入都需要在“声明模块”范围内。感谢@drag13为我指出正确的方向。
有关我遇到的错误的更多信息,我使用了this post作为参考。
答案 0 :(得分:1)
您需要指定您键入所涉及的模块。只需在此处添加模块定义
declare module 'MODULE_NAME' {
class Lipsum {
constructor(argOne: string, argTwo: number)
firstMethod(firstArg: string, secondArg: string): string
secondMethod(): void
}
export default Lipsum;
}