Search code examples
typescriptnpmtyping

Is there any good reason to set the compiler option "declaration" to true in a typescript project


I have a typescript project and it's possible that I want to publish it as an NPM package in the future.

Now at the moment I have set "declaration": true in my tsconfig.json. However this causes me some problems (which don't really matter for this question).

If I set it to false everything works asI want it to.

The declaration flag when set to true, generates d.ts files. However my understanding according to the post About "*.d.ts" in TypeScript is, that this is only relevant if you're not already programming in typescript (i.e. your project is javascript), so that you can later easily use this javascript library in a typescript project, because you now have the types.

So why should anyone ever set this setting to true when the project is "pure" typescript? Can I safely set it to false?


Solution

  • Is there any good reason to set the compiler option “declaration” to true in a typescript project?

    Yes.

    Especially if you are going to publish as an NPM package!

    Justification

    When you create your NPM package, you should include the .js and the .d.ts files - and not the .ts files. This makes your package really portable, and ensures it can be used by both TypeScript projects and plain old JavaScript projects.

    This is the recommended packaging mechanism for TypeScript on NPM and more information on publishing can be found in the handbook.