如何从节点模块导出Typescript接口?

E. *_*erg 5 node.js node-modules typescript rollupjs

弄清楚了!

最初,我试图像这样导入我的模块:

const qml = require('quill-marking-logic')
const { checkSentenceCombining, checkSentenceFragment, checkDiagnosticQuestion, checkFillInTheBlankQuestion, ConceptResult } = qml
Run Code Online (Sandbox Code Playgroud)

因为TS2307: Cannot find module 'quill-marking-logic'尝试使用时出现错误

import { checkSentenceCombining, checkSentenceFragment, checkDiagnosticQuestion, checkFillInTheBlankQuestion, ConceptResult } from 'quill-marking-logic'

这是因为我是用"module": "es6"我的进口程序的tsconfig,默认设置moduleResolution选项Classic。通过将其显式设置为node,我可以使用import语法并获取接口!

原始帖子

我使用Typescript构建了一个节点模块,该模块在另一个应用程序中用作依赖项。我在模块中尝试从其入口点导出几个接口,以便可以在其他应用程序中使用它们,但是在编译后将其删除。我知道这是Typescript设计的一部分,因为这些接口用于运行时分析,但是我想知道是否有一种解决方法,因此我不必在我的其他应用中再次定义它们相同的代码在两个地方。我正在使用汇总作为捆绑程序。

这是我的入口点的.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';
Run Code Online (Sandbox Code Playgroud)

出口的最后一行是接口应该经过的地方。

这是我的tsconfig:

{
    "compilerOptions": {
        "target": "es5",
        "module": "CommonJS",
        "moduleResolution": "node",
        "allowSyntheticDefaultImports": true,
        "sourceMap": false,
        "noImplicitAny": false,
        "lib": [
            "dom",
            "es7"
        ],
        "typeRoots": [
            "node_modules/@types/"
        ],
        "declaration": true
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我尝试将其导入到的应用程序的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'
    }
}
Run Code Online (Sandbox Code Playgroud)

我在package.json的“ typings”键中指向生成的.d.ts文件。

Nat*_*end 7

是的,这是可能的。为此,export您希望在模块的入口点文件中提供给模块使用者的接口:

// in entry.ts
import { MyInterface1 } from '/path/to/interface/one';
import { MyInterface2 } from '/path/to/interface/two';

export { MyInterface1, MyInterface2 };
Run Code Online (Sandbox Code Playgroud)

然后,在使用此模块的代码中,您可以执行以下操作:

import { MyInterface1, MyInterface2 } from 'my-module`;
Run Code Online (Sandbox Code Playgroud)

为了使其工作,您需要确保模块中的declaration编译器选项设置为true- 这会导致编译器输出.d.ts包含模块类型信息的文件。

最后一个难题是types在模块的“package.json”中包含一个指向此.d.ts文件的属性:

{
    "name": "my-module",
    "main": "./entry.js",
    "types": "./my-module.d.ts"
}
Run Code Online (Sandbox Code Playgroud)

有关准备发布模块的更多信息:https : //www.typescriptlang.org/docs/handbook/declaration-files/publishing.html


Tit*_*mir 1

您应该使用该--declaration选项为您的模块生成d.ts文件。定义文件将包含导出的接口,您可以在应用程序中使用它们。

您应该将定义文件与模块一起包含在打字稿在以下位置查找定义的位置

同样,非相对导入将遵循 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 
Run Code Online (Sandbox Code Playgroud)