[英]Cannot use multiple imports from TypeScript npm package
I have a small TypeScript-based npm package with the following tsconfig.json
:我有一个基于 TypeScript 的小型 npm package 带有以下
tsconfig.json
:
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"declaration": true,
"outDir": "./dist",
"strict": true,
"resolveJsonModule": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
},
"include": [
"src/**/*"
]
}
In its src/index.ts
, it exports two classes, one of them being the default:在其
src/index.ts
中,它导出两个类,其中一个是默认类:
import StateMachine from './StateMachine';
import Chunk from './Chunk';
exports.StateMachine = StateMachine;
exports.Chunk = Chunk;
export default StateMachine;
This gets compiled to the following file in dist/index.js
:这被编译到
dist/index.js
中的以下文件:
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const StateMachine_1 = __importDefault(require("./StateMachine"));
const Chunk_1 = __importDefault(require("./Chunk"));
exports.StateMachine = StateMachine_1.default;
exports.Chunk = Chunk_1.default;
exports.default = StateMachine_1.default;
Now, in another project, where I am also using TypeScript, I want to use both of these exported members.现在,在另一个项目中,我也在使用 TypeScript,我想同时使用这两个导出的成员。 So I write:
所以我写:
import StateMachine, { Chunk } from '@foo/bar';
However, I get an error saying that “Module … has no exported member Chunk”.但是,我收到一条错误消息,说“模块……没有导出的成员块”。 The default export however is found without issues.
但是,默认导出没有问题。 This is the
tsconfig.json
of the other project:这是另一个项目的
tsconfig.json
:
{
"compilerOptions": {
"jsx": "react",
"target": "es5",
"module": "esnext",
"noEmitOnError": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"resolveJsonModule": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"stripInternal": false,
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"strict": true,
"noImplicitAny": true,
"declaration": false,
"useDefineForClassFields": true,
"skipLibCheck": true,
},
"include": [
"source",
"webpack.config.js"
]
}
What am I doing wrong here?我在这里做错了什么?
The solution is rather simple.解决方案相当简单。 Do not manually set
exports
, but instead use the following in src/index.ts
:不要手动设置
exports
,而是在src/index.ts
中使用以下内容:
export { StateMachine, Chunk }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.