简体   繁体   English

用于打字的TypeScript外部节点模块声明文件

[英]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: 这是我的问题:

  • How do I do this the right way? 我该怎么做正确的方法?
  • How can I make sure that TypeScript recognizes the default property? 如何确保TypeScript识别default属性?
  • Do I need to use the declare module stuff? 我需要使用declare module东西吗?
  • Do I need to declare the imports ( fs and path )? 我是否需要声明导入( fspath )?

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.

相关问题 为TypeScript声明节点模块,该模块在typings中不可用 - Declare node module for TypeScript that is not available within typings 打字稿:是否需要正确的方式(在节点中)(新的)TYPINGS文件? - Typescript: Require a (new) TYPINGS file the correct way (in node)? Node.js:错误“导出的外部程序包键入文件” - Node.js: Error “Exported external package typings file” 在TypeScript中导入节点并使用typing表达 - Importing node and express with typings in TypeScript 如何为具有构造函数的外部commonjs模块编写TypeScript声明文件? - How do I write a TypeScript declaration file for an external commonjs module that has constructor? 如何为具有构造函数的复杂外部commonjs模块编写TypeScript声明文件,例如imap? - How do I write a TypeScript declaration file for a complex external commonjs module that has constructor, such as imap? Typescript 外部声明文件 npm package - 构造函数 - Typescript declaration file for an external npm package - constructor 如何在Node.js中使用打字稿声明文件要求外部js库 - How to require an external js library in Node.js with a typescript declaration file 导入节点模块的TS类型 - Import TS typings for Node module 类模块的声明文件(TypeScript) - Declaration file for class-module (TypeScript)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM