首页 > 解决方案 > 将 TypeScript 声明编译/复制到输出目录

问题描述

我在 TypeScript 中有一个模块,当它被编译时,它不会复制我的自定义声明文件,例如接口。

配置示例:

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "declaration": true,
    "outDir": "./lib",
    "strict": true,
    "typeRoots": ["node_modules/@types"],
    "esModuleInterop": true
  },
  "include": ["src/**/*.ts"],
  "exclude": ["dist", "lib", "node_modules"]
}

当我编译文件时,声明文件和 JavaScript 文件会在lib文件夹中创建,这要归功于declarationflag。但是,我的自定义界面没有被复制到lib,它们可能很有用。

结构示例:

/lib
 - index.d.ts // It contains an error because it is trying to export interface, and it is not in the folder
 - index.js
 - example.js
 - example.d.ts
 - interface.d.ts // missing!!
/src
 - index.ts // Here is exported my interface and example function
 - example.ts
 - interface.d.ts

我可以删除.d扩展名,但随后,文件将作为 JavaScript 空模块生成。

当我使用 TypeScript 编译时,可以将这些文件直接放在lib文件夹中,或者我应该使用一些命令复制这些文件:excp src/*.d.ts lib/

我不想使用复制方法,因为所有接口都会被导出,我只想导出模块中导出/使用的接口index.d.ts。也许没有其他选择。

标签: javascripttypescriptnode-modules

解决方案


推荐阅读