Ritave Ritave - 3 months ago 16
TypeScript Question

What is the proper way to include Typescript typings for js packages which will be published to npm?

I have 3 NodeJS packages, 2 of which will be published to npm later on and are in javascript, I want for them to have typings included with them. How do I do that properly?

I have one library

common
, one
user-facing
and a sample. Sample depends on
user-facing
, while that one depends on
common
.

I've created typings file for each .js file in each package, I included
"typings"
directive in
package.json
for each package specifying the main typing file.

But when I try to compile my sample
tsc
properly finds the typings for
user-facing
but then complains that it can't find further typings inside the files of
user-facing
which are trying to import
common
.

How can I run my sample locally without publishing the typings to registry beforehand?

Answer

You shouldn't have to publish things to the typings registry for libraries that you create. They can carry their own typings. The typings registry is for creating typings for libraries that others created.

You want to include a typings element in the package.json

Example here

"typings": "./yourentry.d.ts",

And then have the build produce the typing .d.ts files

In your tsconfig:

{ "compilerOptions": {, "declaration": true,

Doing that will allow tsc and vscode to find it and when consumers do an npm install, it will just work for compile time and intellisense.