Is there any advantage of using declaration files over sources in typescript?

339 Views Asked by At

When creating and publishing a library using typescript there are 2 options:

  1. Generate declaration files d.ts together with the bundled javascript file and then in package.json point to it with:

    "types": "./dist/mylib.d.ts"

  2. Don't generate the declaration files and point directly to the source code in package.json witt:

    "types": "./src/index.ts"

What is the adventage of each one?

1

There are 1 best solutions below

0
On

I suggest the following strategy:

Library Package

In the library package, distribute only the complied JavaScript with the declarations files. This has the advantage of reducing the package size while giving developers the option to use your library with either JavaScript or TypeScript.

Code Repository

In the code repository for the library, only commit the source files - not the compiled JavaScript nor the declarations files. Put instructions for how to compile the library in your README, and perhaps also a script that will perform the compilation. This will allow other developers to change and use your library after any modifications they make.