techhub.social is one of the many independent Mastodon servers you can use to participate in the fediverse.
A hub primarily for passionate technologists, but everyone is welcome

Administered by:

Server stats:

4.8K
active users

Doug Parker 🕸️

Neat trick. You can force a `declare global` from another file into the compilation by using:

```typescript
import type {} from './global.js';
```

If the global isn't otherwise in the compilation, this will pull it in and apply the global types.

But because it's `import type`, the import is elided, so there's no runtime impact.

A little weird to import a `global.js` file which doesn't exist (I put the content in a `global.d.ts` file) but it's useful to manage global usage without having to adjust your `tsconfig.json`.

I do wish was stricter about when and where you can use global types. Specifically:

1. Explicit globals: Require `globalThis.foo` to access a global value (not `foo`).
2. Strict dependencies: Require me to _import_ the `declare global` definition from *each* file which references it.

These could help reduce "spooky action at a distance" by forcing devs to be explicit about when and where they are using globals as well as preventing one file from accidentally depending on a global type defined in a completely unrelated file.