-
-
Notifications
You must be signed in to change notification settings - Fork 595
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add GlobalThis
type
#511
Add GlobalThis
type
#511
Conversation
I don't think we should support |
c7dbcd9
to
ed59727
Compare
Fair enough. Just need to guard our codebase from |
ed59727
to
9312081
Compare
This type is for times when you need to access non-standard properties off of `window`, `self`, or `globalThis`. For example: ```ts import type {GlobalThis} from 'type-fest'; interface ExtraProps extends GlobalThis { readonly GLOBAL_TOKEN: string; } const extraProps = window as ExtraProps; ``` --- Normally, you would define a declaration file to add global variables. When you need to do this in a more targeted manner though, a common approach is: ```ts interface ExtraProps extends Window { readonly GLOBAL_TOKEN: string } const extraProps = window as unknown as ExtraProps; ``` Without the cast through `unknown`, you get error `TS2352`. You can try to workaround that with the following: ```ts type GlobalThis = (typeof globalThis) & Window; interface ExtraProps extends GlobalThis { readonly GLOBAL_TOKEN: string } ``` But then you get the error: `Property 'Infinity' of type 'number' is not assignable to numeric index type 'Window'.(2412)` (also for `NaN`). So you have to redefine `Infinity` and `NaN` as `never` to get this working. --- Note: In environments that choose to not load the `"dom"` lib (e.g., for workers or node), the global `Window` interface will not be defined. The only way to modify `Infinity` and `NaN` would be to declare `Window` in the `global` scope. This is undesirable though as now an empty `Window` symbol will be present for users of `type-fest`. The workaround here is to make use of a `@ts-ignore` to treat `Window` as `any` if it is not present.
9312081
to
186c756
Compare
Nice work 👍 |
I don't understand this type. Globals are global, what does "locally scoped properties on |
This type is for times when you need to access non-standard properties off of
window
,self
, orglobalThis
. For example:Normally, you would define a declaration file to add global variables.
This change permits introducing global symbols in a more targeted manner.
Note: In the test, it seems like
expectError
should work here rather than using@ts-expect-error
but, unfortunately, it does not.I'd be happy to hear other suggestions on supporting this use case!