0

Is it good practice to keep all my types in *.d.ts as opposed to *.types.ts? Im trying to separate my type declarations from my actual components and was decided to put them in *.d.ts files. However, it seems *.d.ts serves the purpose of exposing types to javascript.

Lunny
  • 852
  • 1
  • 10
  • 23
  • 1
    Does [this answer](https://stackoverflow.com/questions/56018167/typescript-does-not-copy-d-ts-files-to-build/56440335#56440335) help out? `.d.ts` files do not get copied over to dist folder by TS, so I would consider `.ts` files superior in most cases (especially when you want to expose them as public API). – ford04 Nov 11 '19 at 22:36
  • Ah ok, so *.d.ts files wouldnt get copied but *.types.ts would. You can answer it and ill set it as correct answer. – Lunny Nov 12 '19 at 15:45

1 Answers1

1

In most cases you want to use .ts files. .d.ts files are the result of a compilation with declaration:true option and provide type definitions alongside corresponding .js files, so that JS as well as TS clients can consume the package appropriately.

In addition, the compiler will also use types from .d.ts files (if existent) in the source directory to compile check your project. But it will only treat them as input to the compilation process and will not copy them over to the output folder. So, for the sake of completeness we can make a quick comparison:

+ .ts- files

  • general purpose case and favored most of the time
  • Files are automatically copied over to dist folder, so types are ready to be consumed with public API.
  • No extra build process is necessary to copy .d.ts files over.

+ .d.ts- files

  • compiler-enforced separation between declaration and implementation
  • Types are ensured to remain in the source directory and to be used only internally in the project
ford04
  • 66,267
  • 20
  • 199
  • 171