Skip to content
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

Unable to use global this when noImplicitThis is enabled. #15613

Closed
david-driscoll opened this issue May 5, 2017 · 6 comments
Closed

Unable to use global this when noImplicitThis is enabled. #15613

david-driscoll opened this issue May 5, 2017 · 6 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@david-driscoll
Copy link

When noImplicitThis is turned on, you are unable to reference the global this (which may be window, or global, or other value in various runtime environments).

I'm not sure if there is a recommended way around this, but currently there does not appear to be a way to get the "global" this without an error 'this' implicitly has type 'any' because it does not have a type annotation. This may be something I'm just not seeing. I tried to define global but that wasn't much help.

TypeScript Version: 2.3.x

Code

(function (this: any, root) {
    // do something isolated 
})(<any>this);

Expected behavior:
Be able to use the global this without error.

Actual behavior:
Compiler error 'this' implicitly has type 'any' because it does not have a type annotation. when using this globally.

cc @benlesh

@mhegazy
Copy link
Contributor

mhegazy commented May 5, 2017

The intention here was to not allow use of this in the global scope with --noImplicitThis since there is no way of declaring the type of this globally.

@mhegazy mhegazy added the Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature label May 5, 2017
@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented May 5, 2017

From my response in #3576 which was adapted from this Stack Overflow answer, you could just use Function("return this")();

@benlesh
Copy link

benlesh commented May 9, 2017

Function("return this")()

Ugh.

Can TypeScript start taking a serious look at the global keyword which is stage 3 now?

@mhegazy
Copy link
Contributor

mhegazy commented May 9, 2017

tracked by #12902 and #14052

Also the proposal seems to have stalled on the choice of the variable name; since global breaks existing code.

@RyanCavanaugh RyanCavanaugh added the Suggestion An idea for TypeScript label Aug 20, 2018
@ojanvafai
Copy link

It appears a variable name was chosen (globalThis) and has successfully shipped in Chrome 71 stable channel. https://www.chromestatus.com/features/6571514765770752

@sandersn
Copy link
Member

Fixed by #29332

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

7 participants