[英]TypeScript External Node Module Declaration File For Typings
I have a Node.js module ( index.js
) that has been transpiled with Babel to the code as follows: 我有一个Node.js模块( index.js
)已经使用Babel转换为代码,如下所示:
// My stuff ...
var fs = require("fs");
var path = require("path");
exports.fileContent = fs.readFileSync(
path.join(__dirname, "file.txt"),
'utf8'
);
// Babel compiler stuff ...
Object.defineProperty(
exports,
"__esModule",
{ value: true }
);
// My stuff again ...
exports.default = exports.fileContent;
Usage in Node.js would be: Node.js中的用法是:
var myModule = require("my-module");
doSomethingWithIt( myModule.fileContent );
As far as I understand, I need to create a .d.ts
declaration file and reference it in the typings
field in my package.json
. 据我了解,我需要创建一个.d.ts
声明文件,并引用它在typings
在这个领域package.json
。 And also, this declaration file has to be a module. 此外,此声明文件必须是一个模块。 So my first approach after reading several tutorials about this topic was: 因此,在阅读有关此主题的几个教程后,我的第一个方法是:
// index.d.ts
declare module "my-module" {
export const fileContent: string;
export default fileContent;
}
But sadly, this was a fail: 但遗憾的是,这是一个失败:
[...] error TS2656: Exported external package typings file 'index.d.ts' is not a module. [...]
My next approach was to get rid of the declare module
thing: 我的下一个方法是摆脱declare module
事情:
export const fileContent: string;
export default fileContent;
This works for me, but however feels wrong as I did not find any .d.ts
example file that is not using the declare
stuff. 这对我.d.ts
,但是感觉不对,因为我没有找到任何没有使用declare
内容的.d.ts
示例文件。 I also noticed that I should not use export default
in a namespace/module, which leads me to the point where I don't understand how to declare the default export of my module at all. 我还注意到我不应该在命名空间/模块中使用export default
,这导致我无法理解如何声明模块的默认导出。
Here are my questions: 这是我的问题:
default
property? 如何确保TypeScript识别default
属性? declare module
stuff? 我需要使用declare module
东西吗? fs
and path
)? 我是否需要声明导入( fs
和path
)? Edit: 编辑:
After a bit more researching and fiddling, I think I have found the solution by myself. 经过一番研究和摆弄,我想我已经找到了解决方案。 As far as I understand, the external module description/declaration has to be a module—which means that it has to import or export something: In this case, it has to export the declaration and the declared constant as the default export as well: 据我所知,外部模块描述/声明必须是一个模块 - 这意味着它必须导入或导出一些东西:在这种情况下,它必须将声明和声明的常量导出为默认导出:
export declare const fileContent: string;
export default fileContent;
error TS2656: ... is not a module
happens when trying to use ES6 style imports and the definition does not export a module. error TS2656: ... is not a module
尝试使用ES6样式导入时error TS2656: ... is not a module
,并且定义不导出模块。
I suggest changing your definition to 我建议你改变你的定义
declare module MyModule {
export const fileContent: string;
}
declare module "my-module" {
export default MyModule
}
Since this definition is now "default exporting" a module, it can be imported using the default ES6 import syntax 由于此定义现在是“默认导出”模块,因此可以使用默认的ES6导入语法导入该模块
import MyModule from "my-module"
Then use it 然后使用它
MyModule.fileContent...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.