Seun
Seun

Reputation: 23

Is there really no way to declare types globally without imports and exports?

I have some types declared in my project and I want to know how to use them without exporting and importing them all over the project. I know the index.d.ts works in a similar way for modules within the project, but I'm trying to get that behaviour project-wide.

Upvotes: 2

Views: 2328

Answers (1)

Christian Ivicevic
Christian Ivicevic

Reputation: 10885

In order to have globally available types without explicitly exporting or importing them, you can use a custom definition file something.d.ts (naming is up to you) and declare types inside of them, similar to modules defining their API. The types in the definition file will be available project-wide.


However keep in mind that this method is highly subjective and I personally dislike using this approach since it can be confusing to figure out where types are actually coming from. Especially when there are no explicit imports for used types in your source file. This can be "slightly" handled by namespacing the global types to indicate their origin. But this, as mentioned, is just a personal gripe and highly subjective.

Upvotes: 3

Related Questions