从模块的子文件夹导入打字稿

时间:2018-08-08 15:02:48

标签: typescript

我在Typescript中为自己创建了一个npm模块,其结构如下:

.
| - src/
|   | - index.ts
|   | - types.d.ts
|   | - utils/
|   |   | - util1.ts

这里是内容:

// types.d.ts
export type MyType = string;

---

// index.ts
import { MyType } from './types';

export default (): MyType => 'a string';

---

// utils/util1.ts
export const myFunction = () => 'util string';

然后tsc将这些.ts文件编译到lib/文件夹中,然后该文件夹仅包含.js文件。它保持相同的内部文件夹结构。

在我的主要项目中,我这样做:

import { myFunction } from 'myModule/lib/utils/util1'

但是我得到一个错误

Could not find a declaration file for module 'myModule/lib/util/util1'.

如何将类型添加到lib/文件夹中的已编译JS中?

1 个答案:

答案 0 :(得分:1)

"declaration": true的{​​{1}}文件中设置tsconfig.json将导致myModule生成tsc文件以及{{ 1}}文件夹,它应该能够从主项目中找到这些文件。

相关问题