搞定了!
最开始,我尝试像这样导入我的模块:
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构建了一个Node模块,并将其用作另一个应用程序的依赖项。该模块中有一些接口,我正在尝试从其入口点导出这些接口,以便我可以在其他应用程序中使用它们,但它们在编译后被擦除。我知道这是TypeScript设计的一部分,因为接口用于运行时分析,但我想知道是否有办法绕过它,以便我不必在其他应用程序中再次定义它们并且需要在两个位置维护相同的代码。 我正在使用rollup作为我的打包工具。
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 文件。