Home > Back-end >  Is NPM package's type declaration files must be .d.ts file?
Is NPM package's type declaration files must be .d.ts file?

Time:02-11

In my eyes, we can put type declaration in any typescript file ( such as '.d.ts', '.ts', '.tsx' ).

But I found that almost every npm package's type declaration file is .d.ts file.

Is this necessary?


My real world scene is that I just want my npm package been used by some typescript project, so I publish my package without compile (all .ts files)

I'm not sure is there any problem?

CodePudding user response:

When publishing a package you generate .js files and .d.ts files. This guarantees that people can install your package in a 'normal' JavaScript project and do not have to use TypeScript if they don't want to. They can not interpret .ts or .tsx files. The .d.ts files alongside your .js files can then be ignored.

If you only want to use this package in your own TypeScript projects. You will have to tell your bundler (like Webpack) to also compile this package by using the ts-loader, as a browser cannot interpret TypeScript. Also, if it is a Node.js app, Node itself is a lot faster than ts-node. In my opinion this would be unnecessary overhead.

  • Related