question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Access `global` as a type

See original GitHub issue

Since https://github.com/Microsoft/TypeScript/wiki/What’s-new-in-TypeScript#augmenting-globalmodule-scope-from-modules it’s been possible to augment the global from a module. However, in TypeScript definitions (such as dom.d.ts or node.d.ts) we’re left to create our own “globals”. This results in two issue:

  1. inconsistency with what is actually the global namespace
  2. requiring the declaration of global types multiple times in case it’s used as a “real global” or from window. (browsers) or global. (node)

Instead of this, it’d be really nice to access TypeScript’s global as a type. For instance, in dom.d.ts we could do:

declare var window: global

Or in node.d.ts:

declare var global: global

I couldn’t see any previous issues, but it’s partially related to things like https://github.com/Microsoft/TypeScript/issues/12902 - the difference though is that those issues seems to be tracking adding variables to the TypeScript global scope whereas this would be using the global scope as a type to define these variables themselves.

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:2
  • Comments:9 (7 by maintainers)

github_iconTop GitHub Comments

3reactions
weswighamcommented, Oct 26, 2018

Random update: We pretty much know we want to do this (and talked about it when we added import types), we’re just waiting for https://github.com/tc39/proposal-global (the proposal referenced in #12902) to stabilize with a name for the global-thing, so we can use the same name for the type lookup.

2reactions
ExE-Bosscommented, Oct 26, 2020

This is now supported by using typeof globalThis in a type position.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Create a global variable in TypeScript - Stack Overflow
The solution is to augment global scope. declare global { var ENVIRONMENT: string; } globalThis. ENVIRONMENT = 'PROD'; console.
Read more >
Declaring Global Variables in TypeScript - Marius Schulz
#Declare a Global Variable. Another approach is to declare a global variable using the declare var syntax. This way, we can let TypeScript...
Read more >
How to declare Global Variables in TypeScript | bobbyhadz
To declare a global variable in TypeScript, create a `.d.ts` file and use `declare global{}` to extend the global object with typings for...
Read more >
Documentation - Global .d.ts - TypeScript
Global Libraries. A global library is one that can be accessed from the global scope (i.e. without using any form of import )....
Read more >
3+1 Ways to Declare Global Variables in TypeScript - Webtips
declare var app: { create(): void } // Now you can use without type errors: app · declare global { var app: {...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found