Skip to main content

Don't use "as any" for window's global object

Critical
Clean codeSecurityTypescript

What is it?

The object window is a global object providing variables and functions anywhere in brower's enviromnent. You can retrieve height, width, scroll position etc...

Why apply it?

If you use the window's object with "any", you disable the type checking of typescript. The risk is you can have error when the code is executed

How to fix it?

Declare a global variable to enable type checking

Read more:

https://mariusschulz.com/blog/declaring-global-variables-in-typescript

Examples

Example 1:

Negative

Incorrect implementation that violates the practice.

const example = (window as any).getMyCustomProperties();