jeanpaul62
jeanpaul62

Reputation: 10551

Typescript import from module's subfolder

I created myself a npm modules in Typescript, which has the following structure:

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

And here are the contents:

// 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';

And tsc compiles these .ts files into the lib/ folder, which then only contains .js files. It keeps the same inner folder structure.

In my main project, I do:

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

However I get an error

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

How can I add types to the compiled JS in the lib/ folder?

Upvotes: 2

Views: 5685

Answers (1)

Matt McCutchen
Matt McCutchen

Reputation: 30879

Setting "declaration": true in the tsconfig.json file of myModule will cause tsc to generate .d.ts files alongside the .js files in the lib folder, and it should be able to find these files from the main project.

Upvotes: 1

Related Questions