Accessing Window Object from .tsx file
The syntax <type>
is being deprecated by the ts team. This is because there is too much ambiguity between it and the new jsx
syntax. Instead the ts team introduced the as
operator for type assertions.
So this syntax:
(window as any).things
is more update to date.
This change was done, because basically, its very tough to tell a compiler when is such a syntax relating to a type or a jsx-element. Likewise the notation becomes much harder to read (see below for example):
<Component>
{<String>something}
</Component>
Some more detail can be found here https://basarat.gitbooks.io/typescript/docs/types/type-assertion.html#as-foo-vs-foo and here https://github.com/Microsoft/TypeScript/issues/296
You can use the as
syntax for type assertion. This is the alternate syntax for type assertion as <type>obj
conflicts with JSX syntax:
(window as any).myObject
The above will work, however if you want strong typing consider augmenting the Window
interface to add your property so you will get compile-time type checking:
declare global {
interface Window {
myObject: YourObjectType;
}
}