简体   繁体   中英

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

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM