UNPKG

688 BTypeScriptView Raw
1/**
2Declare locally scoped properties on `globalThis`.
3
4When defining a global variable in a declaration file is inappropriate, it can be helpful to define a `type` or `interface` (say `ExtraGlobals`) with the global variable and then cast `globalThis` via code like `globalThis as unknown as ExtraGlobals`.
5
6Instead of casting through `unknown`, you can update your `type` or `interface` to extend `GlobalThis` and then directly cast `globalThis`.
7
8@example
9```
10import type {GlobalThis} from 'type-fest';
11
12type ExtraGlobals = GlobalThis & {
13 readonly GLOBAL_TOKEN: string;
14};
15
16(globalThis as ExtraGlobals).GLOBAL_TOKEN;
17```
18
19@category Type
20*/
21export type GlobalThis = typeof globalThis;