How do you explicitly set a new property on `window` in TypeScript?
up vote
323
down vote
favorite
I setup global namespaces for my objects by explicitly setting a property on window
.
window.MyNamespace = window.MyNamespace || ;
TypeScript underlines MyNamespace
and complains that:
The property 'MyNamespace' does not exist on value of type 'window'
any"
I can make the code work by declaring MyNamespace
as an ambient variable and dropping the window
explicitness but I don't want to do that.
declare var MyNamespace: any;
MyNamespace = MyNamespace || ;
How can I keep window
in there and make TypeScript happy?
As a side note I find it especially funny that TypeScript complains since it tells me that window
is of type any
which by definitely can contain anything.
typescript
add a comment |
up vote
323
down vote
favorite
I setup global namespaces for my objects by explicitly setting a property on window
.
window.MyNamespace = window.MyNamespace || ;
TypeScript underlines MyNamespace
and complains that:
The property 'MyNamespace' does not exist on value of type 'window'
any"
I can make the code work by declaring MyNamespace
as an ambient variable and dropping the window
explicitness but I don't want to do that.
declare var MyNamespace: any;
MyNamespace = MyNamespace || ;
How can I keep window
in there and make TypeScript happy?
As a side note I find it especially funny that TypeScript complains since it tells me that window
is of type any
which by definitely can contain anything.
typescript
see Link
– MHS
Dec 5 at 6:11
add a comment |
up vote
323
down vote
favorite
up vote
323
down vote
favorite
I setup global namespaces for my objects by explicitly setting a property on window
.
window.MyNamespace = window.MyNamespace || ;
TypeScript underlines MyNamespace
and complains that:
The property 'MyNamespace' does not exist on value of type 'window'
any"
I can make the code work by declaring MyNamespace
as an ambient variable and dropping the window
explicitness but I don't want to do that.
declare var MyNamespace: any;
MyNamespace = MyNamespace || ;
How can I keep window
in there and make TypeScript happy?
As a side note I find it especially funny that TypeScript complains since it tells me that window
is of type any
which by definitely can contain anything.
typescript
I setup global namespaces for my objects by explicitly setting a property on window
.
window.MyNamespace = window.MyNamespace || ;
TypeScript underlines MyNamespace
and complains that:
The property 'MyNamespace' does not exist on value of type 'window'
any"
I can make the code work by declaring MyNamespace
as an ambient variable and dropping the window
explicitness but I don't want to do that.
declare var MyNamespace: any;
MyNamespace = MyNamespace || ;
How can I keep window
in there and make TypeScript happy?
As a side note I find it especially funny that TypeScript complains since it tells me that window
is of type any
which by definitely can contain anything.
typescript
typescript
asked Oct 3 '12 at 13:01
joshuapoehls
8,61994156
8,61994156
see Link
– MHS
Dec 5 at 6:11
add a comment |
see Link
– MHS
Dec 5 at 6:11
see Link
– MHS
Dec 5 at 6:11
see Link
– MHS
Dec 5 at 6:11
add a comment |
15 Answers
15
active
oldest
votes
up vote
320
down vote
accepted
Just found the answer to this in another StackOverflow question's answer.
declare global
interface Window MyNamespace: any;
window.MyNamespace = window.MyNamespace || ;
Basically you need to extend the existing window
interface to tell it about your new property.
35
does not work with the current version of typescript.
– citykid
Apr 1 '13 at 17:24
4
Note the capital W in Window. That tripped me up.
– ajm
Oct 9 '13 at 20:55
2
I couldn't get this to compile with tsc 1.0.1.0. Blake Mitchell's answer did work for me, though.
– Pat
Oct 13 '14 at 17:51
34
Be aware that this only works when declared in a separate.d.ts
file. It does not work when used in a.ts
file that uses imports and exports itself. See this answer.
– cdauth
Mar 9 '16 at 14:21
12
Thedeclare global interface Window ...
works with TypeScript 2.5.2, no need for.d.ts
file as mentioned above
– tanguy_k
Sep 3 '17 at 20:29
|
show 6 more comments
up vote
306
down vote
To keep it dynamic, just use:
(<any>window).MyNamespace
8
Any ideas how to do this in a tsx file?
– velop
Mar 16 '17 at 7:06
3
@velop stackoverflow.com/a/38964459/142098
– Doug
Apr 7 '17 at 18:29
1
I guess this only works without-noImplicitAny
...
– martin
Apr 18 '17 at 19:16
1
@martin: The <any> makes it explicit, so it works just fine, I believe. Others: If you are using Typescript with React or other JSX environment (resulting in TSX syntax) you'll have to useas
instead of<>
. See @david-boyd's answer below.
– Don
Aug 22 '17 at 15:59
9
I had to use (window as any).MyNamespace
– ArsalanDotMe
Sep 19 '17 at 9:37
|
show 2 more comments
up vote
150
down vote
Or...
you can just type:
window['MyNamespace']
and you wont get a compile error and it works the same as typing window.MyNamespace
12
but you will probably get a tslint error... If you have one of course
– smnbbrv
Jan 7 '16 at 12:34
39
This completely flies in the face of strong typing, the whole idea behind TypeScript.
– d512
Jan 28 '16 at 23:55
6
@user1334007 using globals does as well. However, some legacy code requires it.
– nathancahill
Mar 1 '16 at 17:03
@user1334007 this is the example when you need this stuff: andrewhfarmer.com/aws-sdk-with-webpack
– SMSk
Dec 5 '16 at 15:18
3
@Rameshwindow['MyNamespace']()
(just add brackets)
– iBaff
Dec 21 '17 at 18:14
|
show 3 more comments
up vote
115
down vote
Using TSX? None of the other answers were working for me.
Here's what I did:
(window as any).MyNamespace
2
Same as(<any> window).MyNamespace
actually
– Dmitry Parzhitsky
May 15 '17 at 12:17
25
It is the same except when using TSX, because the<any>
gets interpreted as JSX, not a type cast.
– Jake Boone
May 15 '17 at 20:40
add a comment |
up vote
53
down vote
The accepted answer is what I used to use, but with TypeScript 0.9.* it no longer works. The new definition of the Window
interface seems to completely replace the built-in definition, instead of augmenting it.
I have taken to doing this instead:
interface MyWindow extends Window
myFunction(): void;
declare var window: MyWindow;
UPDATE: With TypeScript 0.9.5 the accepted answer is working again.
2
This works also with modules as used by TypeScript 2.0.8. Example:export default class MyClass foo() ... ...
interface MyWindow extends Window mc: MyClass
declare var window: MyWindow
window.mc = new MyClass()
Then you can call foo() e.g. from the Chrome Dev Tools console likemc.foo()
– Martin Majewski
Dec 5 '16 at 16:43
This is a very nice answer if you don't want to declare something as global. On the other side, you need to calldeclare var...
in every file you need.
– Puce
May 25 at 12:23
Plus one for this approach because in this case you don't conflict with the other packages which extend the global window in the monorepo.
– Dmitrii Sorin
Oct 4 at 6:55
add a comment |
up vote
33
down vote
If you need to extend the window
object with a custom type that requires the use of import
you can use the following method:
window.d.ts
import MyInterface from './MyInterface';
declare global
interface Window
propName: MyInterface
See 'Global Augmentation' in the 'Declaration Merging' section of the Handbook: https://www.typescriptlang.org/docs/handbook/declaration-merging.html#global-augmentation
add a comment |
up vote
26
down vote
Global are "evil" :), i think the best way to have also the portability is:
First you export the interface: (eg: ./custom.window.ts)
export interface CustomWindow extends Window
customAttribute: any;
Second you import
import CustomWindow from './custom.window.ts';
Third cast global var window with CustomWindow
declare let window: CustomWindow;
In this way you don't have also red line in different IDE if you use with existent attributes of window object, so at the end try:
window.customAttribute = 'works';
window.location.href = '/works';
Tested with Typescript 2.4.x
1
you might want to expand on why globals are evil. In our case we're using a non standardized API on the window object. It is polyfilled for now. Is that truely evil?
– Mathijs Segers
Nov 7 at 8:01
@MathijsSegers i don't know especially your case but.. about globals are evil is not only about javascript.. is in every language.. some reason are: - You can't apply well design pattern - Memory and performance issue (You have them everywhere, try to attach something to String Object and see what happens when you do new String) - Name collision causing side effect on code.. (especially on javascript that have async nature) I can continue but i think you can image the rest...
– onalbi
Nov 7 at 21:14
1
@onabi I'm literally talking about a feature of the browser. It's globally available since it's the browser. You really would suggest it's still wrong to go for global definitions? I mean we could implement Ponyfills but this would bloat browsers which actually support the feature (e.g. fullscreen api). That said I don't believe that all globals are evil perse.
– Mathijs Segers
Nov 8 at 6:54
@MathijsSegers in my opinion the global variable should be avoid to be used or modified where we can.. is true that window is available since browser exist but is also true that was in mutation all the time.. so if for example we define now window.feature = 'Feature'; and this is used in massive way on code.. what happen if window.feature is added by browsers on all code this feature is override.. anyway i gave a explanation of my sentence is not to go against you.. regards...
– onalbi
Nov 8 at 9:10
add a comment |
up vote
15
down vote
I don't need to do this very often, the only case I have had was when using Redux Devtools with middleware.
I simply did:
const composeEnhancers = (window as any).__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
Or you could do:
let myWindow = window as any;
and then myWindow.myProp = 'my value';
add a comment |
up vote
9
down vote
Most of the other answers are not perfect.
- Some of them just suppress the type inference for shop.
- Some of the others only cares about global variable as namespace, but not as interface/class
I also encounter the similar problem this morning. I tried so many "solutions" on SO, but none of them produce no type error absolutely and enable triggering type jumping in IDE(webstorm or vscode).
Finally, from here
https://github.com/Microsoft/TypeScript/issues/3180#issuecomment-102523512
, I find a reasonable solution to attach typings for global variable which acts as interface/class and namespace both.
Example is below:
// typings.d.ts
declare interface Window
myNamespace?: MyNamespace & typeof MyNamespace
declare interface MyNamespace
somemethod?()
declare namespace MyNamespace
// ...
Now, the code above merges the typings of namespace MyNamespace
and interface MyNamespace
into the global variable myNamespace
(the property of window).
1
Thanks - this is the only one you can seemingly use in an ambient non-module context.
– Tyler Sebastian
Jul 26 '17 at 23:31
add a comment |
up vote
7
down vote
Here's how to do it, if you're using TypeScript Definition Manager!
npm install typings --global
Create typings/custom/window.d.ts
:
interface Window
MyNamespace: any;
declare var window: Window;
Install your custom typing:
typings install file:typings/custom/window.d.ts --save --global
Done, use it! Typescript won't complain anymore:
window.MyNamespace = window.MyNamespace || ;
FWIWtypings
was made obsolete with Typescript 2.0 (mid 2016), and has been archived by the owner.
– mrm
Dec 11 at 2:52
add a comment |
up vote
5
down vote
If you're using the Angular CLI it's really straightforward (tested on CLI RC.0):
src/polyfills.ts
declare global
interface Window
myCustomFn: () => void;
my-custom-utils.ts
window.myCustomFn = function ()
...
;
I'm using IntelliJ, so I also needed to change the following setting in the IDE before my new polyfills picked up:
> File
> Settings
> Languages & Frameworks
> TypeScript
> check 'Use TypeScript Service'.
add a comment |
up vote
5
down vote
After finding answers around, I think this page might be helpful.
https://www.typescriptlang.org/docs/handbook/declaration-merging.html#global-augmentation
Not sure about the history of declaration merging, but it explains why the following could work.
declare global
interface Window MyNamespace: any;
window.MyNamespace = window.MyNamespace || ;
add a comment |
up vote
4
down vote
For reference (this is the correct answer):
Inside a .d.ts
definition file
type MyGlobalFunctionType = (name: string) => void
If you work in the browser,
you add members to the browser's window context by reopening Window's interface:
interface Window
myGlobalFunction: MyGlobalFunctionType
Same idea for NodeJS:
declare module NodeJS
interface Global
myGlobalFunction: MyGlobalFunctionType
Now you declare the root variable (that will actually live on window or global)
declare const myGlobalFunction: MyGlobalFunctionType;
Then in a regular .ts
file, but imported as side-effect, you actually implement it:
global/* or window */.myGlobalFunction = function (name: string)
console.log("Hey !", name);
;
And finally use it elsewhere in the codebase, with either:
global/* or window */.myGlobalFunction("Kevin");
myGlobalFunction("Kevin");
Thanks, this is the best example I found, and working well.
– Nick G.
Nov 7 at 15:37
add a comment |
up vote
2
down vote
I wanted to use this in an Angular (6) library today and it took me a while to get this to work as expected.
In order for my library to use declarations I had to use the d.ts
extention for the file that declares the new properties of the global object.
So in the end, the file ended up with something like:
/path-to-angular-workspace/angular-workspace/projects/angular-library/src/globals.d.ts
Once created, don't forget to expose it in your public_api.ts
.
That did it for me. Hope this helps.
add a comment |
up vote
0
down vote
Make a custom interface extends the Window and add your custom property as optional.
Then, let the customWindow that use the custom interface, but valued with the original window.
It's worked with the typescript@3.1.3.
interface ICustomWindow extends Window
MyNamespace?: any
const customWindow:ICustomWindow = window;
customWindow.MyNamespace = customWindow.MyNamespace
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f12709074%2fhow-do-you-explicitly-set-a-new-property-on-window-in-typescript%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
15 Answers
15
active
oldest
votes
15 Answers
15
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
320
down vote
accepted
Just found the answer to this in another StackOverflow question's answer.
declare global
interface Window MyNamespace: any;
window.MyNamespace = window.MyNamespace || ;
Basically you need to extend the existing window
interface to tell it about your new property.
35
does not work with the current version of typescript.
– citykid
Apr 1 '13 at 17:24
4
Note the capital W in Window. That tripped me up.
– ajm
Oct 9 '13 at 20:55
2
I couldn't get this to compile with tsc 1.0.1.0. Blake Mitchell's answer did work for me, though.
– Pat
Oct 13 '14 at 17:51
34
Be aware that this only works when declared in a separate.d.ts
file. It does not work when used in a.ts
file that uses imports and exports itself. See this answer.
– cdauth
Mar 9 '16 at 14:21
12
Thedeclare global interface Window ...
works with TypeScript 2.5.2, no need for.d.ts
file as mentioned above
– tanguy_k
Sep 3 '17 at 20:29
|
show 6 more comments
up vote
320
down vote
accepted
Just found the answer to this in another StackOverflow question's answer.
declare global
interface Window MyNamespace: any;
window.MyNamespace = window.MyNamespace || ;
Basically you need to extend the existing window
interface to tell it about your new property.
35
does not work with the current version of typescript.
– citykid
Apr 1 '13 at 17:24
4
Note the capital W in Window. That tripped me up.
– ajm
Oct 9 '13 at 20:55
2
I couldn't get this to compile with tsc 1.0.1.0. Blake Mitchell's answer did work for me, though.
– Pat
Oct 13 '14 at 17:51
34
Be aware that this only works when declared in a separate.d.ts
file. It does not work when used in a.ts
file that uses imports and exports itself. See this answer.
– cdauth
Mar 9 '16 at 14:21
12
Thedeclare global interface Window ...
works with TypeScript 2.5.2, no need for.d.ts
file as mentioned above
– tanguy_k
Sep 3 '17 at 20:29
|
show 6 more comments
up vote
320
down vote
accepted
up vote
320
down vote
accepted
Just found the answer to this in another StackOverflow question's answer.
declare global
interface Window MyNamespace: any;
window.MyNamespace = window.MyNamespace || ;
Basically you need to extend the existing window
interface to tell it about your new property.
Just found the answer to this in another StackOverflow question's answer.
declare global
interface Window MyNamespace: any;
window.MyNamespace = window.MyNamespace || ;
Basically you need to extend the existing window
interface to tell it about your new property.
edited Jun 15 '17 at 20:46
Luke
8,30923374
8,30923374
answered Oct 3 '12 at 13:46
joshuapoehls
8,61994156
8,61994156
35
does not work with the current version of typescript.
– citykid
Apr 1 '13 at 17:24
4
Note the capital W in Window. That tripped me up.
– ajm
Oct 9 '13 at 20:55
2
I couldn't get this to compile with tsc 1.0.1.0. Blake Mitchell's answer did work for me, though.
– Pat
Oct 13 '14 at 17:51
34
Be aware that this only works when declared in a separate.d.ts
file. It does not work when used in a.ts
file that uses imports and exports itself. See this answer.
– cdauth
Mar 9 '16 at 14:21
12
Thedeclare global interface Window ...
works with TypeScript 2.5.2, no need for.d.ts
file as mentioned above
– tanguy_k
Sep 3 '17 at 20:29
|
show 6 more comments
35
does not work with the current version of typescript.
– citykid
Apr 1 '13 at 17:24
4
Note the capital W in Window. That tripped me up.
– ajm
Oct 9 '13 at 20:55
2
I couldn't get this to compile with tsc 1.0.1.0. Blake Mitchell's answer did work for me, though.
– Pat
Oct 13 '14 at 17:51
34
Be aware that this only works when declared in a separate.d.ts
file. It does not work when used in a.ts
file that uses imports and exports itself. See this answer.
– cdauth
Mar 9 '16 at 14:21
12
Thedeclare global interface Window ...
works with TypeScript 2.5.2, no need for.d.ts
file as mentioned above
– tanguy_k
Sep 3 '17 at 20:29
35
35
does not work with the current version of typescript.
– citykid
Apr 1 '13 at 17:24
does not work with the current version of typescript.
– citykid
Apr 1 '13 at 17:24
4
4
Note the capital W in Window. That tripped me up.
– ajm
Oct 9 '13 at 20:55
Note the capital W in Window. That tripped me up.
– ajm
Oct 9 '13 at 20:55
2
2
I couldn't get this to compile with tsc 1.0.1.0. Blake Mitchell's answer did work for me, though.
– Pat
Oct 13 '14 at 17:51
I couldn't get this to compile with tsc 1.0.1.0. Blake Mitchell's answer did work for me, though.
– Pat
Oct 13 '14 at 17:51
34
34
Be aware that this only works when declared in a separate
.d.ts
file. It does not work when used in a .ts
file that uses imports and exports itself. See this answer.– cdauth
Mar 9 '16 at 14:21
Be aware that this only works when declared in a separate
.d.ts
file. It does not work when used in a .ts
file that uses imports and exports itself. See this answer.– cdauth
Mar 9 '16 at 14:21
12
12
The
declare global interface Window ...
works with TypeScript 2.5.2, no need for .d.ts
file as mentioned above– tanguy_k
Sep 3 '17 at 20:29
The
declare global interface Window ...
works with TypeScript 2.5.2, no need for .d.ts
file as mentioned above– tanguy_k
Sep 3 '17 at 20:29
|
show 6 more comments
up vote
306
down vote
To keep it dynamic, just use:
(<any>window).MyNamespace
8
Any ideas how to do this in a tsx file?
– velop
Mar 16 '17 at 7:06
3
@velop stackoverflow.com/a/38964459/142098
– Doug
Apr 7 '17 at 18:29
1
I guess this only works without-noImplicitAny
...
– martin
Apr 18 '17 at 19:16
1
@martin: The <any> makes it explicit, so it works just fine, I believe. Others: If you are using Typescript with React or other JSX environment (resulting in TSX syntax) you'll have to useas
instead of<>
. See @david-boyd's answer below.
– Don
Aug 22 '17 at 15:59
9
I had to use (window as any).MyNamespace
– ArsalanDotMe
Sep 19 '17 at 9:37
|
show 2 more comments
up vote
306
down vote
To keep it dynamic, just use:
(<any>window).MyNamespace
8
Any ideas how to do this in a tsx file?
– velop
Mar 16 '17 at 7:06
3
@velop stackoverflow.com/a/38964459/142098
– Doug
Apr 7 '17 at 18:29
1
I guess this only works without-noImplicitAny
...
– martin
Apr 18 '17 at 19:16
1
@martin: The <any> makes it explicit, so it works just fine, I believe. Others: If you are using Typescript with React or other JSX environment (resulting in TSX syntax) you'll have to useas
instead of<>
. See @david-boyd's answer below.
– Don
Aug 22 '17 at 15:59
9
I had to use (window as any).MyNamespace
– ArsalanDotMe
Sep 19 '17 at 9:37
|
show 2 more comments
up vote
306
down vote
up vote
306
down vote
To keep it dynamic, just use:
(<any>window).MyNamespace
To keep it dynamic, just use:
(<any>window).MyNamespace
edited Oct 14 '16 at 8:26
answered Jun 9 '15 at 19:18
chinupson
3,2961117
3,2961117
8
Any ideas how to do this in a tsx file?
– velop
Mar 16 '17 at 7:06
3
@velop stackoverflow.com/a/38964459/142098
– Doug
Apr 7 '17 at 18:29
1
I guess this only works without-noImplicitAny
...
– martin
Apr 18 '17 at 19:16
1
@martin: The <any> makes it explicit, so it works just fine, I believe. Others: If you are using Typescript with React or other JSX environment (resulting in TSX syntax) you'll have to useas
instead of<>
. See @david-boyd's answer below.
– Don
Aug 22 '17 at 15:59
9
I had to use (window as any).MyNamespace
– ArsalanDotMe
Sep 19 '17 at 9:37
|
show 2 more comments
8
Any ideas how to do this in a tsx file?
– velop
Mar 16 '17 at 7:06
3
@velop stackoverflow.com/a/38964459/142098
– Doug
Apr 7 '17 at 18:29
1
I guess this only works without-noImplicitAny
...
– martin
Apr 18 '17 at 19:16
1
@martin: The <any> makes it explicit, so it works just fine, I believe. Others: If you are using Typescript with React or other JSX environment (resulting in TSX syntax) you'll have to useas
instead of<>
. See @david-boyd's answer below.
– Don
Aug 22 '17 at 15:59
9
I had to use (window as any).MyNamespace
– ArsalanDotMe
Sep 19 '17 at 9:37
8
8
Any ideas how to do this in a tsx file?
– velop
Mar 16 '17 at 7:06
Any ideas how to do this in a tsx file?
– velop
Mar 16 '17 at 7:06
3
3
@velop stackoverflow.com/a/38964459/142098
– Doug
Apr 7 '17 at 18:29
@velop stackoverflow.com/a/38964459/142098
– Doug
Apr 7 '17 at 18:29
1
1
I guess this only works without
-noImplicitAny
...– martin
Apr 18 '17 at 19:16
I guess this only works without
-noImplicitAny
...– martin
Apr 18 '17 at 19:16
1
1
@martin: The <any> makes it explicit, so it works just fine, I believe. Others: If you are using Typescript with React or other JSX environment (resulting in TSX syntax) you'll have to use
as
instead of <>
. See @david-boyd's answer below.– Don
Aug 22 '17 at 15:59
@martin: The <any> makes it explicit, so it works just fine, I believe. Others: If you are using Typescript with React or other JSX environment (resulting in TSX syntax) you'll have to use
as
instead of <>
. See @david-boyd's answer below.– Don
Aug 22 '17 at 15:59
9
9
I had to use (window as any).MyNamespace
– ArsalanDotMe
Sep 19 '17 at 9:37
I had to use (window as any).MyNamespace
– ArsalanDotMe
Sep 19 '17 at 9:37
|
show 2 more comments
up vote
150
down vote
Or...
you can just type:
window['MyNamespace']
and you wont get a compile error and it works the same as typing window.MyNamespace
12
but you will probably get a tslint error... If you have one of course
– smnbbrv
Jan 7 '16 at 12:34
39
This completely flies in the face of strong typing, the whole idea behind TypeScript.
– d512
Jan 28 '16 at 23:55
6
@user1334007 using globals does as well. However, some legacy code requires it.
– nathancahill
Mar 1 '16 at 17:03
@user1334007 this is the example when you need this stuff: andrewhfarmer.com/aws-sdk-with-webpack
– SMSk
Dec 5 '16 at 15:18
3
@Rameshwindow['MyNamespace']()
(just add brackets)
– iBaff
Dec 21 '17 at 18:14
|
show 3 more comments
up vote
150
down vote
Or...
you can just type:
window['MyNamespace']
and you wont get a compile error and it works the same as typing window.MyNamespace
12
but you will probably get a tslint error... If you have one of course
– smnbbrv
Jan 7 '16 at 12:34
39
This completely flies in the face of strong typing, the whole idea behind TypeScript.
– d512
Jan 28 '16 at 23:55
6
@user1334007 using globals does as well. However, some legacy code requires it.
– nathancahill
Mar 1 '16 at 17:03
@user1334007 this is the example when you need this stuff: andrewhfarmer.com/aws-sdk-with-webpack
– SMSk
Dec 5 '16 at 15:18
3
@Rameshwindow['MyNamespace']()
(just add brackets)
– iBaff
Dec 21 '17 at 18:14
|
show 3 more comments
up vote
150
down vote
up vote
150
down vote
Or...
you can just type:
window['MyNamespace']
and you wont get a compile error and it works the same as typing window.MyNamespace
Or...
you can just type:
window['MyNamespace']
and you wont get a compile error and it works the same as typing window.MyNamespace
answered Nov 20 '12 at 19:36
Evan Larsen
8,18043657
8,18043657
12
but you will probably get a tslint error... If you have one of course
– smnbbrv
Jan 7 '16 at 12:34
39
This completely flies in the face of strong typing, the whole idea behind TypeScript.
– d512
Jan 28 '16 at 23:55
6
@user1334007 using globals does as well. However, some legacy code requires it.
– nathancahill
Mar 1 '16 at 17:03
@user1334007 this is the example when you need this stuff: andrewhfarmer.com/aws-sdk-with-webpack
– SMSk
Dec 5 '16 at 15:18
3
@Rameshwindow['MyNamespace']()
(just add brackets)
– iBaff
Dec 21 '17 at 18:14
|
show 3 more comments
12
but you will probably get a tslint error... If you have one of course
– smnbbrv
Jan 7 '16 at 12:34
39
This completely flies in the face of strong typing, the whole idea behind TypeScript.
– d512
Jan 28 '16 at 23:55
6
@user1334007 using globals does as well. However, some legacy code requires it.
– nathancahill
Mar 1 '16 at 17:03
@user1334007 this is the example when you need this stuff: andrewhfarmer.com/aws-sdk-with-webpack
– SMSk
Dec 5 '16 at 15:18
3
@Rameshwindow['MyNamespace']()
(just add brackets)
– iBaff
Dec 21 '17 at 18:14
12
12
but you will probably get a tslint error... If you have one of course
– smnbbrv
Jan 7 '16 at 12:34
but you will probably get a tslint error... If you have one of course
– smnbbrv
Jan 7 '16 at 12:34
39
39
This completely flies in the face of strong typing, the whole idea behind TypeScript.
– d512
Jan 28 '16 at 23:55
This completely flies in the face of strong typing, the whole idea behind TypeScript.
– d512
Jan 28 '16 at 23:55
6
6
@user1334007 using globals does as well. However, some legacy code requires it.
– nathancahill
Mar 1 '16 at 17:03
@user1334007 using globals does as well. However, some legacy code requires it.
– nathancahill
Mar 1 '16 at 17:03
@user1334007 this is the example when you need this stuff: andrewhfarmer.com/aws-sdk-with-webpack
– SMSk
Dec 5 '16 at 15:18
@user1334007 this is the example when you need this stuff: andrewhfarmer.com/aws-sdk-with-webpack
– SMSk
Dec 5 '16 at 15:18
3
3
@Ramesh
window['MyNamespace']()
(just add brackets)– iBaff
Dec 21 '17 at 18:14
@Ramesh
window['MyNamespace']()
(just add brackets)– iBaff
Dec 21 '17 at 18:14
|
show 3 more comments
up vote
115
down vote
Using TSX? None of the other answers were working for me.
Here's what I did:
(window as any).MyNamespace
2
Same as(<any> window).MyNamespace
actually
– Dmitry Parzhitsky
May 15 '17 at 12:17
25
It is the same except when using TSX, because the<any>
gets interpreted as JSX, not a type cast.
– Jake Boone
May 15 '17 at 20:40
add a comment |
up vote
115
down vote
Using TSX? None of the other answers were working for me.
Here's what I did:
(window as any).MyNamespace
2
Same as(<any> window).MyNamespace
actually
– Dmitry Parzhitsky
May 15 '17 at 12:17
25
It is the same except when using TSX, because the<any>
gets interpreted as JSX, not a type cast.
– Jake Boone
May 15 '17 at 20:40
add a comment |
up vote
115
down vote
up vote
115
down vote
Using TSX? None of the other answers were working for me.
Here's what I did:
(window as any).MyNamespace
Using TSX? None of the other answers were working for me.
Here's what I did:
(window as any).MyNamespace
edited Nov 30 '16 at 20:13
answered Aug 15 '16 at 23:25
David Boyd
4,09431513
4,09431513
2
Same as(<any> window).MyNamespace
actually
– Dmitry Parzhitsky
May 15 '17 at 12:17
25
It is the same except when using TSX, because the<any>
gets interpreted as JSX, not a type cast.
– Jake Boone
May 15 '17 at 20:40
add a comment |
2
Same as(<any> window).MyNamespace
actually
– Dmitry Parzhitsky
May 15 '17 at 12:17
25
It is the same except when using TSX, because the<any>
gets interpreted as JSX, not a type cast.
– Jake Boone
May 15 '17 at 20:40
2
2
Same as
(<any> window).MyNamespace
actually– Dmitry Parzhitsky
May 15 '17 at 12:17
Same as
(<any> window).MyNamespace
actually– Dmitry Parzhitsky
May 15 '17 at 12:17
25
25
It is the same except when using TSX, because the
<any>
gets interpreted as JSX, not a type cast.– Jake Boone
May 15 '17 at 20:40
It is the same except when using TSX, because the
<any>
gets interpreted as JSX, not a type cast.– Jake Boone
May 15 '17 at 20:40
add a comment |
up vote
53
down vote
The accepted answer is what I used to use, but with TypeScript 0.9.* it no longer works. The new definition of the Window
interface seems to completely replace the built-in definition, instead of augmenting it.
I have taken to doing this instead:
interface MyWindow extends Window
myFunction(): void;
declare var window: MyWindow;
UPDATE: With TypeScript 0.9.5 the accepted answer is working again.
2
This works also with modules as used by TypeScript 2.0.8. Example:export default class MyClass foo() ... ...
interface MyWindow extends Window mc: MyClass
declare var window: MyWindow
window.mc = new MyClass()
Then you can call foo() e.g. from the Chrome Dev Tools console likemc.foo()
– Martin Majewski
Dec 5 '16 at 16:43
This is a very nice answer if you don't want to declare something as global. On the other side, you need to calldeclare var...
in every file you need.
– Puce
May 25 at 12:23
Plus one for this approach because in this case you don't conflict with the other packages which extend the global window in the monorepo.
– Dmitrii Sorin
Oct 4 at 6:55
add a comment |
up vote
53
down vote
The accepted answer is what I used to use, but with TypeScript 0.9.* it no longer works. The new definition of the Window
interface seems to completely replace the built-in definition, instead of augmenting it.
I have taken to doing this instead:
interface MyWindow extends Window
myFunction(): void;
declare var window: MyWindow;
UPDATE: With TypeScript 0.9.5 the accepted answer is working again.
2
This works also with modules as used by TypeScript 2.0.8. Example:export default class MyClass foo() ... ...
interface MyWindow extends Window mc: MyClass
declare var window: MyWindow
window.mc = new MyClass()
Then you can call foo() e.g. from the Chrome Dev Tools console likemc.foo()
– Martin Majewski
Dec 5 '16 at 16:43
This is a very nice answer if you don't want to declare something as global. On the other side, you need to calldeclare var...
in every file you need.
– Puce
May 25 at 12:23
Plus one for this approach because in this case you don't conflict with the other packages which extend the global window in the monorepo.
– Dmitrii Sorin
Oct 4 at 6:55
add a comment |
up vote
53
down vote
up vote
53
down vote
The accepted answer is what I used to use, but with TypeScript 0.9.* it no longer works. The new definition of the Window
interface seems to completely replace the built-in definition, instead of augmenting it.
I have taken to doing this instead:
interface MyWindow extends Window
myFunction(): void;
declare var window: MyWindow;
UPDATE: With TypeScript 0.9.5 the accepted answer is working again.
The accepted answer is what I used to use, but with TypeScript 0.9.* it no longer works. The new definition of the Window
interface seems to completely replace the built-in definition, instead of augmenting it.
I have taken to doing this instead:
interface MyWindow extends Window
myFunction(): void;
declare var window: MyWindow;
UPDATE: With TypeScript 0.9.5 the accepted answer is working again.
edited Feb 20 '14 at 18:37
answered Aug 27 '13 at 21:22
Blake Mitchell
1,84211620
1,84211620
2
This works also with modules as used by TypeScript 2.0.8. Example:export default class MyClass foo() ... ...
interface MyWindow extends Window mc: MyClass
declare var window: MyWindow
window.mc = new MyClass()
Then you can call foo() e.g. from the Chrome Dev Tools console likemc.foo()
– Martin Majewski
Dec 5 '16 at 16:43
This is a very nice answer if you don't want to declare something as global. On the other side, you need to calldeclare var...
in every file you need.
– Puce
May 25 at 12:23
Plus one for this approach because in this case you don't conflict with the other packages which extend the global window in the monorepo.
– Dmitrii Sorin
Oct 4 at 6:55
add a comment |
2
This works also with modules as used by TypeScript 2.0.8. Example:export default class MyClass foo() ... ...
interface MyWindow extends Window mc: MyClass
declare var window: MyWindow
window.mc = new MyClass()
Then you can call foo() e.g. from the Chrome Dev Tools console likemc.foo()
– Martin Majewski
Dec 5 '16 at 16:43
This is a very nice answer if you don't want to declare something as global. On the other side, you need to calldeclare var...
in every file you need.
– Puce
May 25 at 12:23
Plus one for this approach because in this case you don't conflict with the other packages which extend the global window in the monorepo.
– Dmitrii Sorin
Oct 4 at 6:55
2
2
This works also with modules as used by TypeScript 2.0.8. Example:
export default class MyClass foo() ... ...
interface MyWindow extends Window mc: MyClass
declare var window: MyWindow
window.mc = new MyClass()
Then you can call foo() e.g. from the Chrome Dev Tools console like mc.foo()
– Martin Majewski
Dec 5 '16 at 16:43
This works also with modules as used by TypeScript 2.0.8. Example:
export default class MyClass foo() ... ...
interface MyWindow extends Window mc: MyClass
declare var window: MyWindow
window.mc = new MyClass()
Then you can call foo() e.g. from the Chrome Dev Tools console like mc.foo()
– Martin Majewski
Dec 5 '16 at 16:43
This is a very nice answer if you don't want to declare something as global. On the other side, you need to call
declare var...
in every file you need.– Puce
May 25 at 12:23
This is a very nice answer if you don't want to declare something as global. On the other side, you need to call
declare var...
in every file you need.– Puce
May 25 at 12:23
Plus one for this approach because in this case you don't conflict with the other packages which extend the global window in the monorepo.
– Dmitrii Sorin
Oct 4 at 6:55
Plus one for this approach because in this case you don't conflict with the other packages which extend the global window in the monorepo.
– Dmitrii Sorin
Oct 4 at 6:55
add a comment |
up vote
33
down vote
If you need to extend the window
object with a custom type that requires the use of import
you can use the following method:
window.d.ts
import MyInterface from './MyInterface';
declare global
interface Window
propName: MyInterface
See 'Global Augmentation' in the 'Declaration Merging' section of the Handbook: https://www.typescriptlang.org/docs/handbook/declaration-merging.html#global-augmentation
add a comment |
up vote
33
down vote
If you need to extend the window
object with a custom type that requires the use of import
you can use the following method:
window.d.ts
import MyInterface from './MyInterface';
declare global
interface Window
propName: MyInterface
See 'Global Augmentation' in the 'Declaration Merging' section of the Handbook: https://www.typescriptlang.org/docs/handbook/declaration-merging.html#global-augmentation
add a comment |
up vote
33
down vote
up vote
33
down vote
If you need to extend the window
object with a custom type that requires the use of import
you can use the following method:
window.d.ts
import MyInterface from './MyInterface';
declare global
interface Window
propName: MyInterface
See 'Global Augmentation' in the 'Declaration Merging' section of the Handbook: https://www.typescriptlang.org/docs/handbook/declaration-merging.html#global-augmentation
If you need to extend the window
object with a custom type that requires the use of import
you can use the following method:
window.d.ts
import MyInterface from './MyInterface';
declare global
interface Window
propName: MyInterface
See 'Global Augmentation' in the 'Declaration Merging' section of the Handbook: https://www.typescriptlang.org/docs/handbook/declaration-merging.html#global-augmentation
answered Oct 23 '16 at 15:24
Lucifer Sam
3,37633457
3,37633457
add a comment |
add a comment |
up vote
26
down vote
Global are "evil" :), i think the best way to have also the portability is:
First you export the interface: (eg: ./custom.window.ts)
export interface CustomWindow extends Window
customAttribute: any;
Second you import
import CustomWindow from './custom.window.ts';
Third cast global var window with CustomWindow
declare let window: CustomWindow;
In this way you don't have also red line in different IDE if you use with existent attributes of window object, so at the end try:
window.customAttribute = 'works';
window.location.href = '/works';
Tested with Typescript 2.4.x
1
you might want to expand on why globals are evil. In our case we're using a non standardized API on the window object. It is polyfilled for now. Is that truely evil?
– Mathijs Segers
Nov 7 at 8:01
@MathijsSegers i don't know especially your case but.. about globals are evil is not only about javascript.. is in every language.. some reason are: - You can't apply well design pattern - Memory and performance issue (You have them everywhere, try to attach something to String Object and see what happens when you do new String) - Name collision causing side effect on code.. (especially on javascript that have async nature) I can continue but i think you can image the rest...
– onalbi
Nov 7 at 21:14
1
@onabi I'm literally talking about a feature of the browser. It's globally available since it's the browser. You really would suggest it's still wrong to go for global definitions? I mean we could implement Ponyfills but this would bloat browsers which actually support the feature (e.g. fullscreen api). That said I don't believe that all globals are evil perse.
– Mathijs Segers
Nov 8 at 6:54
@MathijsSegers in my opinion the global variable should be avoid to be used or modified where we can.. is true that window is available since browser exist but is also true that was in mutation all the time.. so if for example we define now window.feature = 'Feature'; and this is used in massive way on code.. what happen if window.feature is added by browsers on all code this feature is override.. anyway i gave a explanation of my sentence is not to go against you.. regards...
– onalbi
Nov 8 at 9:10
add a comment |
up vote
26
down vote
Global are "evil" :), i think the best way to have also the portability is:
First you export the interface: (eg: ./custom.window.ts)
export interface CustomWindow extends Window
customAttribute: any;
Second you import
import CustomWindow from './custom.window.ts';
Third cast global var window with CustomWindow
declare let window: CustomWindow;
In this way you don't have also red line in different IDE if you use with existent attributes of window object, so at the end try:
window.customAttribute = 'works';
window.location.href = '/works';
Tested with Typescript 2.4.x
1
you might want to expand on why globals are evil. In our case we're using a non standardized API on the window object. It is polyfilled for now. Is that truely evil?
– Mathijs Segers
Nov 7 at 8:01
@MathijsSegers i don't know especially your case but.. about globals are evil is not only about javascript.. is in every language.. some reason are: - You can't apply well design pattern - Memory and performance issue (You have them everywhere, try to attach something to String Object and see what happens when you do new String) - Name collision causing side effect on code.. (especially on javascript that have async nature) I can continue but i think you can image the rest...
– onalbi
Nov 7 at 21:14
1
@onabi I'm literally talking about a feature of the browser. It's globally available since it's the browser. You really would suggest it's still wrong to go for global definitions? I mean we could implement Ponyfills but this would bloat browsers which actually support the feature (e.g. fullscreen api). That said I don't believe that all globals are evil perse.
– Mathijs Segers
Nov 8 at 6:54
@MathijsSegers in my opinion the global variable should be avoid to be used or modified where we can.. is true that window is available since browser exist but is also true that was in mutation all the time.. so if for example we define now window.feature = 'Feature'; and this is used in massive way on code.. what happen if window.feature is added by browsers on all code this feature is override.. anyway i gave a explanation of my sentence is not to go against you.. regards...
– onalbi
Nov 8 at 9:10
add a comment |
up vote
26
down vote
up vote
26
down vote
Global are "evil" :), i think the best way to have also the portability is:
First you export the interface: (eg: ./custom.window.ts)
export interface CustomWindow extends Window
customAttribute: any;
Second you import
import CustomWindow from './custom.window.ts';
Third cast global var window with CustomWindow
declare let window: CustomWindow;
In this way you don't have also red line in different IDE if you use with existent attributes of window object, so at the end try:
window.customAttribute = 'works';
window.location.href = '/works';
Tested with Typescript 2.4.x
Global are "evil" :), i think the best way to have also the portability is:
First you export the interface: (eg: ./custom.window.ts)
export interface CustomWindow extends Window
customAttribute: any;
Second you import
import CustomWindow from './custom.window.ts';
Third cast global var window with CustomWindow
declare let window: CustomWindow;
In this way you don't have also red line in different IDE if you use with existent attributes of window object, so at the end try:
window.customAttribute = 'works';
window.location.href = '/works';
Tested with Typescript 2.4.x
edited Feb 26 at 10:49
Yukulélé
5,35923658
5,35923658
answered Jul 27 '17 at 13:28
onalbi
1,0921529
1,0921529
1
you might want to expand on why globals are evil. In our case we're using a non standardized API on the window object. It is polyfilled for now. Is that truely evil?
– Mathijs Segers
Nov 7 at 8:01
@MathijsSegers i don't know especially your case but.. about globals are evil is not only about javascript.. is in every language.. some reason are: - You can't apply well design pattern - Memory and performance issue (You have them everywhere, try to attach something to String Object and see what happens when you do new String) - Name collision causing side effect on code.. (especially on javascript that have async nature) I can continue but i think you can image the rest...
– onalbi
Nov 7 at 21:14
1
@onabi I'm literally talking about a feature of the browser. It's globally available since it's the browser. You really would suggest it's still wrong to go for global definitions? I mean we could implement Ponyfills but this would bloat browsers which actually support the feature (e.g. fullscreen api). That said I don't believe that all globals are evil perse.
– Mathijs Segers
Nov 8 at 6:54
@MathijsSegers in my opinion the global variable should be avoid to be used or modified where we can.. is true that window is available since browser exist but is also true that was in mutation all the time.. so if for example we define now window.feature = 'Feature'; and this is used in massive way on code.. what happen if window.feature is added by browsers on all code this feature is override.. anyway i gave a explanation of my sentence is not to go against you.. regards...
– onalbi
Nov 8 at 9:10
add a comment |
1
you might want to expand on why globals are evil. In our case we're using a non standardized API on the window object. It is polyfilled for now. Is that truely evil?
– Mathijs Segers
Nov 7 at 8:01
@MathijsSegers i don't know especially your case but.. about globals are evil is not only about javascript.. is in every language.. some reason are: - You can't apply well design pattern - Memory and performance issue (You have them everywhere, try to attach something to String Object and see what happens when you do new String) - Name collision causing side effect on code.. (especially on javascript that have async nature) I can continue but i think you can image the rest...
– onalbi
Nov 7 at 21:14
1
@onabi I'm literally talking about a feature of the browser. It's globally available since it's the browser. You really would suggest it's still wrong to go for global definitions? I mean we could implement Ponyfills but this would bloat browsers which actually support the feature (e.g. fullscreen api). That said I don't believe that all globals are evil perse.
– Mathijs Segers
Nov 8 at 6:54
@MathijsSegers in my opinion the global variable should be avoid to be used or modified where we can.. is true that window is available since browser exist but is also true that was in mutation all the time.. so if for example we define now window.feature = 'Feature'; and this is used in massive way on code.. what happen if window.feature is added by browsers on all code this feature is override.. anyway i gave a explanation of my sentence is not to go against you.. regards...
– onalbi
Nov 8 at 9:10
1
1
you might want to expand on why globals are evil. In our case we're using a non standardized API on the window object. It is polyfilled for now. Is that truely evil?
– Mathijs Segers
Nov 7 at 8:01
you might want to expand on why globals are evil. In our case we're using a non standardized API on the window object. It is polyfilled for now. Is that truely evil?
– Mathijs Segers
Nov 7 at 8:01
@MathijsSegers i don't know especially your case but.. about globals are evil is not only about javascript.. is in every language.. some reason are: - You can't apply well design pattern - Memory and performance issue (You have them everywhere, try to attach something to String Object and see what happens when you do new String) - Name collision causing side effect on code.. (especially on javascript that have async nature) I can continue but i think you can image the rest...
– onalbi
Nov 7 at 21:14
@MathijsSegers i don't know especially your case but.. about globals are evil is not only about javascript.. is in every language.. some reason are: - You can't apply well design pattern - Memory and performance issue (You have them everywhere, try to attach something to String Object and see what happens when you do new String) - Name collision causing side effect on code.. (especially on javascript that have async nature) I can continue but i think you can image the rest...
– onalbi
Nov 7 at 21:14
1
1
@onabi I'm literally talking about a feature of the browser. It's globally available since it's the browser. You really would suggest it's still wrong to go for global definitions? I mean we could implement Ponyfills but this would bloat browsers which actually support the feature (e.g. fullscreen api). That said I don't believe that all globals are evil perse.
– Mathijs Segers
Nov 8 at 6:54
@onabi I'm literally talking about a feature of the browser. It's globally available since it's the browser. You really would suggest it's still wrong to go for global definitions? I mean we could implement Ponyfills but this would bloat browsers which actually support the feature (e.g. fullscreen api). That said I don't believe that all globals are evil perse.
– Mathijs Segers
Nov 8 at 6:54
@MathijsSegers in my opinion the global variable should be avoid to be used or modified where we can.. is true that window is available since browser exist but is also true that was in mutation all the time.. so if for example we define now window.feature = 'Feature'; and this is used in massive way on code.. what happen if window.feature is added by browsers on all code this feature is override.. anyway i gave a explanation of my sentence is not to go against you.. regards...
– onalbi
Nov 8 at 9:10
@MathijsSegers in my opinion the global variable should be avoid to be used or modified where we can.. is true that window is available since browser exist but is also true that was in mutation all the time.. so if for example we define now window.feature = 'Feature'; and this is used in massive way on code.. what happen if window.feature is added by browsers on all code this feature is override.. anyway i gave a explanation of my sentence is not to go against you.. regards...
– onalbi
Nov 8 at 9:10
add a comment |
up vote
15
down vote
I don't need to do this very often, the only case I have had was when using Redux Devtools with middleware.
I simply did:
const composeEnhancers = (window as any).__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
Or you could do:
let myWindow = window as any;
and then myWindow.myProp = 'my value';
add a comment |
up vote
15
down vote
I don't need to do this very often, the only case I have had was when using Redux Devtools with middleware.
I simply did:
const composeEnhancers = (window as any).__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
Or you could do:
let myWindow = window as any;
and then myWindow.myProp = 'my value';
add a comment |
up vote
15
down vote
up vote
15
down vote
I don't need to do this very often, the only case I have had was when using Redux Devtools with middleware.
I simply did:
const composeEnhancers = (window as any).__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
Or you could do:
let myWindow = window as any;
and then myWindow.myProp = 'my value';
I don't need to do this very often, the only case I have had was when using Redux Devtools with middleware.
I simply did:
const composeEnhancers = (window as any).__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
Or you could do:
let myWindow = window as any;
and then myWindow.myProp = 'my value';
edited Jun 13 at 8:27
answered Jun 6 at 13:13
Dimitar Nikovski
44369
44369
add a comment |
add a comment |
up vote
9
down vote
Most of the other answers are not perfect.
- Some of them just suppress the type inference for shop.
- Some of the others only cares about global variable as namespace, but not as interface/class
I also encounter the similar problem this morning. I tried so many "solutions" on SO, but none of them produce no type error absolutely and enable triggering type jumping in IDE(webstorm or vscode).
Finally, from here
https://github.com/Microsoft/TypeScript/issues/3180#issuecomment-102523512
, I find a reasonable solution to attach typings for global variable which acts as interface/class and namespace both.
Example is below:
// typings.d.ts
declare interface Window
myNamespace?: MyNamespace & typeof MyNamespace
declare interface MyNamespace
somemethod?()
declare namespace MyNamespace
// ...
Now, the code above merges the typings of namespace MyNamespace
and interface MyNamespace
into the global variable myNamespace
(the property of window).
1
Thanks - this is the only one you can seemingly use in an ambient non-module context.
– Tyler Sebastian
Jul 26 '17 at 23:31
add a comment |
up vote
9
down vote
Most of the other answers are not perfect.
- Some of them just suppress the type inference for shop.
- Some of the others only cares about global variable as namespace, but not as interface/class
I also encounter the similar problem this morning. I tried so many "solutions" on SO, but none of them produce no type error absolutely and enable triggering type jumping in IDE(webstorm or vscode).
Finally, from here
https://github.com/Microsoft/TypeScript/issues/3180#issuecomment-102523512
, I find a reasonable solution to attach typings for global variable which acts as interface/class and namespace both.
Example is below:
// typings.d.ts
declare interface Window
myNamespace?: MyNamespace & typeof MyNamespace
declare interface MyNamespace
somemethod?()
declare namespace MyNamespace
// ...
Now, the code above merges the typings of namespace MyNamespace
and interface MyNamespace
into the global variable myNamespace
(the property of window).
1
Thanks - this is the only one you can seemingly use in an ambient non-module context.
– Tyler Sebastian
Jul 26 '17 at 23:31
add a comment |
up vote
9
down vote
up vote
9
down vote
Most of the other answers are not perfect.
- Some of them just suppress the type inference for shop.
- Some of the others only cares about global variable as namespace, but not as interface/class
I also encounter the similar problem this morning. I tried so many "solutions" on SO, but none of them produce no type error absolutely and enable triggering type jumping in IDE(webstorm or vscode).
Finally, from here
https://github.com/Microsoft/TypeScript/issues/3180#issuecomment-102523512
, I find a reasonable solution to attach typings for global variable which acts as interface/class and namespace both.
Example is below:
// typings.d.ts
declare interface Window
myNamespace?: MyNamespace & typeof MyNamespace
declare interface MyNamespace
somemethod?()
declare namespace MyNamespace
// ...
Now, the code above merges the typings of namespace MyNamespace
and interface MyNamespace
into the global variable myNamespace
(the property of window).
Most of the other answers are not perfect.
- Some of them just suppress the type inference for shop.
- Some of the others only cares about global variable as namespace, but not as interface/class
I also encounter the similar problem this morning. I tried so many "solutions" on SO, but none of them produce no type error absolutely and enable triggering type jumping in IDE(webstorm or vscode).
Finally, from here
https://github.com/Microsoft/TypeScript/issues/3180#issuecomment-102523512
, I find a reasonable solution to attach typings for global variable which acts as interface/class and namespace both.
Example is below:
// typings.d.ts
declare interface Window
myNamespace?: MyNamespace & typeof MyNamespace
declare interface MyNamespace
somemethod?()
declare namespace MyNamespace
// ...
Now, the code above merges the typings of namespace MyNamespace
and interface MyNamespace
into the global variable myNamespace
(the property of window).
answered May 19 '17 at 3:26
e-cloud
2,6811231
2,6811231
1
Thanks - this is the only one you can seemingly use in an ambient non-module context.
– Tyler Sebastian
Jul 26 '17 at 23:31
add a comment |
1
Thanks - this is the only one you can seemingly use in an ambient non-module context.
– Tyler Sebastian
Jul 26 '17 at 23:31
1
1
Thanks - this is the only one you can seemingly use in an ambient non-module context.
– Tyler Sebastian
Jul 26 '17 at 23:31
Thanks - this is the only one you can seemingly use in an ambient non-module context.
– Tyler Sebastian
Jul 26 '17 at 23:31
add a comment |
up vote
7
down vote
Here's how to do it, if you're using TypeScript Definition Manager!
npm install typings --global
Create typings/custom/window.d.ts
:
interface Window
MyNamespace: any;
declare var window: Window;
Install your custom typing:
typings install file:typings/custom/window.d.ts --save --global
Done, use it! Typescript won't complain anymore:
window.MyNamespace = window.MyNamespace || ;
FWIWtypings
was made obsolete with Typescript 2.0 (mid 2016), and has been archived by the owner.
– mrm
Dec 11 at 2:52
add a comment |
up vote
7
down vote
Here's how to do it, if you're using TypeScript Definition Manager!
npm install typings --global
Create typings/custom/window.d.ts
:
interface Window
MyNamespace: any;
declare var window: Window;
Install your custom typing:
typings install file:typings/custom/window.d.ts --save --global
Done, use it! Typescript won't complain anymore:
window.MyNamespace = window.MyNamespace || ;
FWIWtypings
was made obsolete with Typescript 2.0 (mid 2016), and has been archived by the owner.
– mrm
Dec 11 at 2:52
add a comment |
up vote
7
down vote
up vote
7
down vote
Here's how to do it, if you're using TypeScript Definition Manager!
npm install typings --global
Create typings/custom/window.d.ts
:
interface Window
MyNamespace: any;
declare var window: Window;
Install your custom typing:
typings install file:typings/custom/window.d.ts --save --global
Done, use it! Typescript won't complain anymore:
window.MyNamespace = window.MyNamespace || ;
Here's how to do it, if you're using TypeScript Definition Manager!
npm install typings --global
Create typings/custom/window.d.ts
:
interface Window
MyNamespace: any;
declare var window: Window;
Install your custom typing:
typings install file:typings/custom/window.d.ts --save --global
Done, use it! Typescript won't complain anymore:
window.MyNamespace = window.MyNamespace || ;
answered Nov 19 '16 at 21:31
Nik Sumeiko
2,85673044
2,85673044
FWIWtypings
was made obsolete with Typescript 2.0 (mid 2016), and has been archived by the owner.
– mrm
Dec 11 at 2:52
add a comment |
FWIWtypings
was made obsolete with Typescript 2.0 (mid 2016), and has been archived by the owner.
– mrm
Dec 11 at 2:52
FWIW
typings
was made obsolete with Typescript 2.0 (mid 2016), and has been archived by the owner.– mrm
Dec 11 at 2:52
FWIW
typings
was made obsolete with Typescript 2.0 (mid 2016), and has been archived by the owner.– mrm
Dec 11 at 2:52
add a comment |
up vote
5
down vote
If you're using the Angular CLI it's really straightforward (tested on CLI RC.0):
src/polyfills.ts
declare global
interface Window
myCustomFn: () => void;
my-custom-utils.ts
window.myCustomFn = function ()
...
;
I'm using IntelliJ, so I also needed to change the following setting in the IDE before my new polyfills picked up:
> File
> Settings
> Languages & Frameworks
> TypeScript
> check 'Use TypeScript Service'.
add a comment |
up vote
5
down vote
If you're using the Angular CLI it's really straightforward (tested on CLI RC.0):
src/polyfills.ts
declare global
interface Window
myCustomFn: () => void;
my-custom-utils.ts
window.myCustomFn = function ()
...
;
I'm using IntelliJ, so I also needed to change the following setting in the IDE before my new polyfills picked up:
> File
> Settings
> Languages & Frameworks
> TypeScript
> check 'Use TypeScript Service'.
add a comment |
up vote
5
down vote
up vote
5
down vote
If you're using the Angular CLI it's really straightforward (tested on CLI RC.0):
src/polyfills.ts
declare global
interface Window
myCustomFn: () => void;
my-custom-utils.ts
window.myCustomFn = function ()
...
;
I'm using IntelliJ, so I also needed to change the following setting in the IDE before my new polyfills picked up:
> File
> Settings
> Languages & Frameworks
> TypeScript
> check 'Use TypeScript Service'.
If you're using the Angular CLI it's really straightforward (tested on CLI RC.0):
src/polyfills.ts
declare global
interface Window
myCustomFn: () => void;
my-custom-utils.ts
window.myCustomFn = function ()
...
;
I'm using IntelliJ, so I also needed to change the following setting in the IDE before my new polyfills picked up:
> File
> Settings
> Languages & Frameworks
> TypeScript
> check 'Use TypeScript Service'.
edited Mar 22 '17 at 6:28
answered Mar 21 '17 at 13:38
Stephen Paul
14.6k84744
14.6k84744
add a comment |
add a comment |
up vote
5
down vote
After finding answers around, I think this page might be helpful.
https://www.typescriptlang.org/docs/handbook/declaration-merging.html#global-augmentation
Not sure about the history of declaration merging, but it explains why the following could work.
declare global
interface Window MyNamespace: any;
window.MyNamespace = window.MyNamespace || ;
add a comment |
up vote
5
down vote
After finding answers around, I think this page might be helpful.
https://www.typescriptlang.org/docs/handbook/declaration-merging.html#global-augmentation
Not sure about the history of declaration merging, but it explains why the following could work.
declare global
interface Window MyNamespace: any;
window.MyNamespace = window.MyNamespace || ;
add a comment |
up vote
5
down vote
up vote
5
down vote
After finding answers around, I think this page might be helpful.
https://www.typescriptlang.org/docs/handbook/declaration-merging.html#global-augmentation
Not sure about the history of declaration merging, but it explains why the following could work.
declare global
interface Window MyNamespace: any;
window.MyNamespace = window.MyNamespace || ;
After finding answers around, I think this page might be helpful.
https://www.typescriptlang.org/docs/handbook/declaration-merging.html#global-augmentation
Not sure about the history of declaration merging, but it explains why the following could work.
declare global
interface Window MyNamespace: any;
window.MyNamespace = window.MyNamespace || ;
edited May 18 at 11:47
Sagar Kharche
621620
621620
answered Mar 16 '17 at 17:38
Sheng
14427
14427
add a comment |
add a comment |
up vote
4
down vote
For reference (this is the correct answer):
Inside a .d.ts
definition file
type MyGlobalFunctionType = (name: string) => void
If you work in the browser,
you add members to the browser's window context by reopening Window's interface:
interface Window
myGlobalFunction: MyGlobalFunctionType
Same idea for NodeJS:
declare module NodeJS
interface Global
myGlobalFunction: MyGlobalFunctionType
Now you declare the root variable (that will actually live on window or global)
declare const myGlobalFunction: MyGlobalFunctionType;
Then in a regular .ts
file, but imported as side-effect, you actually implement it:
global/* or window */.myGlobalFunction = function (name: string)
console.log("Hey !", name);
;
And finally use it elsewhere in the codebase, with either:
global/* or window */.myGlobalFunction("Kevin");
myGlobalFunction("Kevin");
Thanks, this is the best example I found, and working well.
– Nick G.
Nov 7 at 15:37
add a comment |
up vote
4
down vote
For reference (this is the correct answer):
Inside a .d.ts
definition file
type MyGlobalFunctionType = (name: string) => void
If you work in the browser,
you add members to the browser's window context by reopening Window's interface:
interface Window
myGlobalFunction: MyGlobalFunctionType
Same idea for NodeJS:
declare module NodeJS
interface Global
myGlobalFunction: MyGlobalFunctionType
Now you declare the root variable (that will actually live on window or global)
declare const myGlobalFunction: MyGlobalFunctionType;
Then in a regular .ts
file, but imported as side-effect, you actually implement it:
global/* or window */.myGlobalFunction = function (name: string)
console.log("Hey !", name);
;
And finally use it elsewhere in the codebase, with either:
global/* or window */.myGlobalFunction("Kevin");
myGlobalFunction("Kevin");
Thanks, this is the best example I found, and working well.
– Nick G.
Nov 7 at 15:37
add a comment |
up vote
4
down vote
up vote
4
down vote
For reference (this is the correct answer):
Inside a .d.ts
definition file
type MyGlobalFunctionType = (name: string) => void
If you work in the browser,
you add members to the browser's window context by reopening Window's interface:
interface Window
myGlobalFunction: MyGlobalFunctionType
Same idea for NodeJS:
declare module NodeJS
interface Global
myGlobalFunction: MyGlobalFunctionType
Now you declare the root variable (that will actually live on window or global)
declare const myGlobalFunction: MyGlobalFunctionType;
Then in a regular .ts
file, but imported as side-effect, you actually implement it:
global/* or window */.myGlobalFunction = function (name: string)
console.log("Hey !", name);
;
And finally use it elsewhere in the codebase, with either:
global/* or window */.myGlobalFunction("Kevin");
myGlobalFunction("Kevin");
For reference (this is the correct answer):
Inside a .d.ts
definition file
type MyGlobalFunctionType = (name: string) => void
If you work in the browser,
you add members to the browser's window context by reopening Window's interface:
interface Window
myGlobalFunction: MyGlobalFunctionType
Same idea for NodeJS:
declare module NodeJS
interface Global
myGlobalFunction: MyGlobalFunctionType
Now you declare the root variable (that will actually live on window or global)
declare const myGlobalFunction: MyGlobalFunctionType;
Then in a regular .ts
file, but imported as side-effect, you actually implement it:
global/* or window */.myGlobalFunction = function (name: string)
console.log("Hey !", name);
;
And finally use it elsewhere in the codebase, with either:
global/* or window */.myGlobalFunction("Kevin");
myGlobalFunction("Kevin");
answered Apr 20 '17 at 15:50
Benoit B.
2,1431315
2,1431315
Thanks, this is the best example I found, and working well.
– Nick G.
Nov 7 at 15:37
add a comment |
Thanks, this is the best example I found, and working well.
– Nick G.
Nov 7 at 15:37
Thanks, this is the best example I found, and working well.
– Nick G.
Nov 7 at 15:37
Thanks, this is the best example I found, and working well.
– Nick G.
Nov 7 at 15:37
add a comment |
up vote
2
down vote
I wanted to use this in an Angular (6) library today and it took me a while to get this to work as expected.
In order for my library to use declarations I had to use the d.ts
extention for the file that declares the new properties of the global object.
So in the end, the file ended up with something like:
/path-to-angular-workspace/angular-workspace/projects/angular-library/src/globals.d.ts
Once created, don't forget to expose it in your public_api.ts
.
That did it for me. Hope this helps.
add a comment |
up vote
2
down vote
I wanted to use this in an Angular (6) library today and it took me a while to get this to work as expected.
In order for my library to use declarations I had to use the d.ts
extention for the file that declares the new properties of the global object.
So in the end, the file ended up with something like:
/path-to-angular-workspace/angular-workspace/projects/angular-library/src/globals.d.ts
Once created, don't forget to expose it in your public_api.ts
.
That did it for me. Hope this helps.
add a comment |
up vote
2
down vote
up vote
2
down vote
I wanted to use this in an Angular (6) library today and it took me a while to get this to work as expected.
In order for my library to use declarations I had to use the d.ts
extention for the file that declares the new properties of the global object.
So in the end, the file ended up with something like:
/path-to-angular-workspace/angular-workspace/projects/angular-library/src/globals.d.ts
Once created, don't forget to expose it in your public_api.ts
.
That did it for me. Hope this helps.
I wanted to use this in an Angular (6) library today and it took me a while to get this to work as expected.
In order for my library to use declarations I had to use the d.ts
extention for the file that declares the new properties of the global object.
So in the end, the file ended up with something like:
/path-to-angular-workspace/angular-workspace/projects/angular-library/src/globals.d.ts
Once created, don't forget to expose it in your public_api.ts
.
That did it for me. Hope this helps.
answered Aug 1 at 11:29
Dirk
213
213
add a comment |
add a comment |
up vote
0
down vote
Make a custom interface extends the Window and add your custom property as optional.
Then, let the customWindow that use the custom interface, but valued with the original window.
It's worked with the typescript@3.1.3.
interface ICustomWindow extends Window
MyNamespace?: any
const customWindow:ICustomWindow = window;
customWindow.MyNamespace = customWindow.MyNamespace
add a comment |
up vote
0
down vote
Make a custom interface extends the Window and add your custom property as optional.
Then, let the customWindow that use the custom interface, but valued with the original window.
It's worked with the typescript@3.1.3.
interface ICustomWindow extends Window
MyNamespace?: any
const customWindow:ICustomWindow = window;
customWindow.MyNamespace = customWindow.MyNamespace
add a comment |
up vote
0
down vote
up vote
0
down vote
Make a custom interface extends the Window and add your custom property as optional.
Then, let the customWindow that use the custom interface, but valued with the original window.
It's worked with the typescript@3.1.3.
interface ICustomWindow extends Window
MyNamespace?: any
const customWindow:ICustomWindow = window;
customWindow.MyNamespace = customWindow.MyNamespace
Make a custom interface extends the Window and add your custom property as optional.
Then, let the customWindow that use the custom interface, but valued with the original window.
It's worked with the typescript@3.1.3.
interface ICustomWindow extends Window
MyNamespace?: any
const customWindow:ICustomWindow = window;
customWindow.MyNamespace = customWindow.MyNamespace
answered Nov 12 at 7:29
shuizhongyuemin
340210
340210
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f12709074%2fhow-do-you-explicitly-set-a-new-property-on-window-in-typescript%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
see Link
– MHS
Dec 5 at 6:11