Skip to content

Isomorphic global augmentation #12208

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

Closed
alfaproject opened this issue Nov 13, 2016 · 5 comments
Closed

Isomorphic global augmentation #12208

alfaproject opened this issue Nov 13, 2016 · 5 comments
Labels
Duplicate An existing issue was already created

Comments

@alfaproject
Copy link

Currently, there is the need to add a lot of boilerplate code if we want to target more than one global environment, and I was wondering if TypeScript should sugar this a bit.

TypeScript Version: ^2.0.0

Code

declare global {
  // Augment Node.js `global`
  namespace NodeJS {
    interface Global {
      wtf: WTF;
    }
  }

  // Augment Browser `window`
  interface Window {
    wtf: WTF;
  }

  // Augment Web Worker `self`
  interface WorkerGlobalScope {
    wtf: WTF;
  }
}

// Get the appropriate global object
const root: NodeJS.Global|Window|WorkerGlobalScope =
  (typeof self === 'object' && self.self === self && self) ||
  (typeof global === 'object' && global.global === global && global) ||
  this;

// Get the augmented property
const wtf = root.wtf;

Expected behavior:
TypeScript to detect the appropriate global environment, maybe by giving the compiler some sort of isomorphic flag.

Actual behavior:
A lot of boilerplate code to have an isomorphic framework.

@aluanhaddad
Copy link
Contributor

Why not use this?

declare global {

  // Augment Node.js `global`
  namespace NodeJS {
    interface Global {
      wtf: WTF;
    }
  }
  // Augment Browser `window`
  interface Window extends NodeJS.Global { }
  // Augment Web Worker `self`
  interface WorkerGlobalScope extends NodeJS.Global { }
}

@alfaproject
Copy link
Author

My main gripe is actually with what comes after that. ):

@chicoxyzzy
Copy link
Contributor

No reason for TypeScript to handle this. There is proposal to ECMAScript with polyfill

@alfaproject
Copy link
Author

Pretty sure TS already polyfills much bigger stuff than this, even things on stage 2. (;
I'd even go as far as having typings for a common shared set of global properties where each environment extends (common Global interface of sorts, even if it's empty)

@mhegazy
Copy link
Contributor

mhegazy commented Dec 14, 2016

I have filed issue #12902 to track implementing the proposal in https://github.com/tc39/proposal-global

I think this would fall from it.

@mhegazy mhegazy added the Duplicate An existing issue was already created label Dec 14, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

5 participants