【发布时间】:2018-07-25 05:13:51
【问题描述】:
想通了!
最初,我试图像这样导入我的模块:
const qml = require('quill-marking-logic')
const { checkSentenceCombining, checkSentenceFragment, checkDiagnosticQuestion, checkFillInTheBlankQuestion, ConceptResult } = qml
因为我在尝试使用时遇到了TS2307: Cannot find module 'quill-marking-logic' 错误
import { checkSentenceCombining, checkSentenceFragment, checkDiagnosticQuestion, checkFillInTheBlankQuestion, ConceptResult } from 'quill-marking-logic'
这是因为我在导入应用程序的 tsconfig 中使用了"module": "es6",默认情况下将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';
最后一行是接口应该通过的地方。
这是我的 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 文件。
【问题讨论】:
标签: node.js typescript node-modules rollupjs