【发布时间】:2020-07-05 10:06:08
【问题描述】:
如何使用定义文件创建 NPM 包,其中仅在 *.ts 文件中声明接口。
假设我们有两个接口和一个类定义:
export interface A {
id: number;
}
export interface B {
name: string;
}
export class C {
}
我需要将这些 *.ts 文件打包到 npm 包中,该怎么做?我应该在index.ts 中导出它们吗?
我的package.json 是:
{
"name": "npm",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
我的tsconfig.json 是:
"compilerOptions": {
"target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
"module": "commonjs",
"strict": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true
}
在index.ts里面有:
import { A } from "./a";
import { B } from "./b";
import { C } from "./c";
'./a', './b', './c' 是带有接口和类声明的文件。
当我使用命令将它构建到index.js 文件时:tsc index.ts 然后我无法在其他项目中使用模块index.js 访问接口(npm install)
【问题讨论】:
标签: javascript node.js typescript npm