Node.js 如何从节点模块导出Typescript接口?
找到了答案强> 最初,我试图像这样导入我的模块:Node.js 如何从节点模块导出Typescript接口?,node.js,typescript,node-modules,rollupjs,Node.js,Typescript,Node Modules,Rollupjs,找到了答案 最初,我试图像这样导入我的模块: const qml = require('quill-marking-logic') const { checkSentenceCombining, checkSentenceFragment, checkDiagnosticQuestion, checkFillInTheBlankQuestion, ConceptResult } = qml 因为我在尝试使用时遇到一个TS2307:找不到模块'quill marking logic'错误 imp
const qml = require('quill-marking-logic')
const { checkSentenceCombining, checkSentenceFragment, checkDiagnosticQuestion, checkFillInTheBlankQuestion, ConceptResult } = qml
因为我在尝试使用时遇到一个TS2307:找不到模块'quill marking logic'
错误
import{checksentencecombing,checkSentenceFragment,checkdiagnosticquestics,checkfillinblankquestion,ConceptResult}来自“羽毛笔标记逻辑”
这是因为我在导入应用程序的tsconfig中使用了“module”:“es6”
,默认情况下将moduleResolution
选项设置为Classic
。通过显式地将其设置为节点
,我能够使用导入
语法并获得我的界面
原创帖子
我已经使用Typescript构建了一个节点模块,我在另一个应用程序中将其用作依赖项。我在模块中有几个接口,我正试图从its入口点导出这些接口,以便我可以在我的其他应用程序中使用它们,但编译后它们会被删除。我知道这是Typescript设计的一部分,因为接口用于运行时分析,但我想知道是否有办法绕过它,这样我就不必在我的其他应用程序中再次定义它们,并且必须在两个地方维护相同的代码。我正在使用rollup作为绑定器
这就是.d.ts版本的“我的入口点”的外观:
export { checkSentenceCombining } from './libs/graders/sentence_combining';
export { checkDiagnosticQuestion } from './libs/graders/diagnostic_question';
export { checkSentenceFragment } from './libs/graders/sentence_fragment';
export { checkFillInTheBlankQuestion } from './libs/graders/fill_in_the_blank';
export { Response, PartialResponse, ConceptResult, FocusPoint, IncorrectSequence, FeedbackObject, GradingObject, WordCountChange } from './interfaces/index';
最后一行导出是接口应该通过的地方
这是我的tsconfig:
{
"compilerOptions": {
"target": "es5",
"module": "CommonJS",
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"sourceMap": false,
"noImplicitAny": false,
"lib": [
"dom",
"es7"
],
"typeRoots": [
"node_modules/@types/"
],
"declaration": true
}
}
这是我尝试将其导入的应用程序的tsconfig:
{
"compilerOptions": {
"outDir": "./dist/", // path to output directory
"sourceMap": true, // allow sourcemap support
"strictNullChecks": true, // enable strict null checks as a best practice
"module": "es6", // specifiy module code generation
"jsx": "react", // use typescript to transpile jsx to js
"target": "es6", // specify ECMAScript target version
"allowJs": true, // allow a partial TypeScript and JavaScript codebase
"lib": ["ES2017", "DOM"], //
"allowSyntheticDefaultImports": true // Allow import React from 'react'
}
}
我指向我的package.json中“typings”键中生成的.d.ts文件。您应该使用--declaration
选项为您的模块生成d.ts
文件。定义文件将包含导出的接口,您可以在应用程序中使用它们
您应该将模块中的定义文件包括在typescript查找定义的位置中:
类似地,非相对导入将遵循Node.js解析逻辑,首先查找文件,然后查找适用的文件夹。因此,从源文件/root/src/moduleA.ts中的“moduleB”导入{b}将导致以下查找:
/root/src/node_modules/moduleB.ts
/root/src/node_modules/moduleB.tsx
/root/src/node_modules/moduleB.d.ts
/root/src/node_modules/moduleB/package.json (if it specifies a "types" property)
/root/src/node_modules/moduleB/index.ts
/root/src/node_modules/moduleB/index.tsx
/root/src/node_modules/moduleB/index.d.ts
是的,这是可能的。为此,
export
在模块的入口点文件中,导出要向模块使用者提供的接口:
// in entry.ts
import { MyInterface1 } from '/path/to/interface/one';
import { MyInterface2 } from '/path/to/interface/two';
export { MyInterface1, MyInterface2 };
然后,在使用此模块的代码中,您可以执行以下操作:
import { MyInterface1, MyInterface2 } from 'my-module`;
为了使其正常工作,您需要确保模块中的声明
编译器选项设置为true
——这会导致编译器输出包含模块键入信息的.d.ts
文件
最后一个难题是在模块的“package.json”中包含一个types
属性,该属性指向此.d.ts
文件:
{
"name": "my-module",
"main": "./entry.js",
"types": "./my-module.d.ts"
}
"allowJs": false,
"declaration": true,
"types": "dist/index.d.ts",
有关准备要发布的模块的详细信息,请执行以下操作:
将其添加到您的
tsconfig.json
文件中:
{
"name": "my-module",
"main": "./entry.js",
"types": "./my-module.d.ts"
}
"allowJs": false,
"declaration": true,
"types": "dist/index.d.ts",
将此添加到您的package.json
文件:
{
"name": "my-module",
"main": "./entry.js",
"types": "./my-module.d.ts"
}
"allowJs": false,
"declaration": true,
"types": "dist/index.d.ts",
另外,不要忘了将d.ts
文件放在一个单独的文件夹中,然后在package.json
中发布,否则它们将丢失。例如,将它们放在src
文件夹旁边的typings
文件夹中,并将其添加到package.json
"files": [
"/dist",
"/typings"
],
更多详情:谢谢提香!我在tsconfig中使用了--declaration,并在my package.json中指定了.d.ts文件,但接口似乎仍然没有通过。@E.Friedberg是从模块导出的接口吗?我编辑了原始帖子,以显示我的入口点是什么样子。接口无法通过。嘿,内森,谢谢你的帮助!我已经更新了我的原始帖子,以反映我拥有您在这里指出的所有代码,但有一个例外:包的“type”和“typings”键之间是否存在差异。json?@E.Friedberg:不,您可以使用其中任何一个-它们是同义词。