Home:ALL Converter>typescript: using declaration files in a typescript project to expose types globally

typescript: using declaration files in a typescript project to expose types globally

Ask Time:2020-08-02T22:30:48         Author:gaurav5430

Json Formatter

I have recently started working with typescript. I have a typescript project setup which has a lot of React components written with typescript. These typescript components usually export the component itself and to make development convenient, I have extracted all the types required by this typescript component to a separate file. Now usually you would have to import these types in your component to use it, but since it was cumbersome, some one in the team came up with an idea to name these separate file containing types as types.d.ts (or whatever.d.ts). This makes the types available globally across the project, without needing to explicitly import the types anywhere.

Now, I understand that type declaration files are only required in case I am consuming a javascript component in my typescript project, and if I want to have type checking working for that javascript component. In which case, I can either get the type declaration file from outside (like, DefinitelyTyped) , or I can create a local declaration file. This is not what i am using a declaration file here for.

But i would like to understand what are the problems with this usage?

  • Is it wrong to expose all the types globally (Please note that most of these types are only required internally by the component, only some are required by other components which consume this component)?
  • Is there going to be any problem with the compiler or the typescript setup due to this ?
  • I have read about a compiler option which generates the declaration files, can this setup interfere with that ?

NOTE: This separate file which is named as d.ts does not really follow any specific guidelines that may be required for declaration files. This just has some regular interface declarations

Author:gaurav5430,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/63217178/typescript-using-declaration-files-in-a-typescript-project-to-expose-types-glob
yy