'How do I make TypeScript styles and types available without explicit import?

I'm a bit new to the TypeScript (JavaScript, React Native, etc.) ecosystem and am having trouble figuring out what some current best practices are. I'm especially unsure about the best way to organize and expose type definitions.

For example, my preference is that types and interfaces should be defined in modules that define associated functionality (that is, not in some global file) but should be automatically availalbe to all code that imports that module, without having to explicitly import the type definitions.

In order to achieve this, a typical module has something like

// SomeModule
export const someThing = {
    // ...
}

declare global {

  interface SomeInterface {
      // ...
  }

  type SomeType {
      // ...
  }
}

But I'm not sure this is the right way to go about things.

I'm aware that I could achieve something similar with a single types.d.ts file in which I placed all type definitions, but I'm very uncomfortable with doing this since it separates the definitions of the types from the module where the associate functionality is defined.

The situation is similar, though somewhat different, in the case of styles. In that case, IMV, it is crucial that there be a single source of truth and that every part of a project be exposed to any definitions or changes to styles, regardless of whether they import any styles definitions. I'm not sure how to achieve that though.

So (if you're still with me) the objectives boil down to:

  1. Define types in the module where associated functionality is defined, and make type definitions available automatically to any file importing the module where they are defined.
  2. Define styles centrally and make style definitions automatically available to all code in the project, without explicit import being necessary.

And my question is:

  1. Are these objectives viewed as good TypeScript practice (in the context of a React Native app, if that matters)?
  2. What is the best way to achieve these objectives?


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source