我在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中?
答案 0 :(得分:1)
在"declaration": true
的{{1}}文件中设置tsconfig.json
将导致myModule
生成tsc
文件以及{{ 1}}文件夹,它应该能够从主项目中找到这些文件。