3

I have some code within which I'm using typeof window == "undefined" to check whether there is a browser environement. When I'm launching this code with ts-node, I'm getting this:

typings/Console.ts:36:10 - error TS2304: Cannot find name 'window'.

36      typeof window == "undefined"
               ~~~~~~

AFAIK typeof is kind of operator that is safe to use with not defined variables, and it works well both in browser and in NodeJS environement. But as far as I start to use it with ts-node, it starts to throw.

My tsconfig.json

{
    "compilerOptions": {
        "module": "CommonJS",
        "target": "es5",
        "moduleResolution": "node",
        "baseUrl": "src",
        "allowSyntheticDefaultImports": true,
        "noImplicitAny": true,
        "strict": false,
        "sourceMap": true,
        "traceResolution": true,
        "experimentalDecorators": true,
        "emitDecoratorMetadata": true,
        "strictNullChecks": true,

        "allowJs": false,
        "declaration": false,
        "removeComments": true,
        "noLib": false,
        "preserveConstEnums": true,
        "suppressImplicitAnyIndexErrors": true,
        "types": [ "node" ],
        "lib": [ "es6" ],
        "downlevelIteration": true,
        "resolveJsonModule": true,
        "typeRoots": [
            "../node_modules/@types"
        ]
    }
}

So, what the trick? Thanks in advance!

5
  • you could try typeof (window || undefined) == "undefined" Commented May 18, 2020 at 13:57
  • 1
    try add to lib in tsconfig "DOM" Commented May 18, 2020 at 13:58
  • 1
    @LukaKostic This won't help because window is still not defined. Commented May 18, 2020 at 13:59
  • @kalit well, don't understand why, but it helped. Seems more like a bug than like an expected behavior. Thank you! Post your comment as answer to allow me to mark it as right answer :) Commented May 18, 2020 at 14:02
  • @Limbo but there is only check type is defined, not object, maybe somewhere later is defined Commented May 18, 2020 at 14:03

2 Answers 2

5

try add to lib in tsconfig "DOM"

Sign up to request clarification or add additional context in comments.

1 Comment

It allows developers to use other DOM-specific, unrelated types by mistake.
4

For me it worked to first declare the variable to TypeScript, so:

declare var window;

if(typeof window == "undefined"){
// code
}

2 Comments

Hello and thank you for your answer. Unfortunately, this approach is not good when your project is basically browser oriented and you have several similar conditions across the sources (e.g. typeof document != "undefined", typeof navigator != "undefined, etc.). As answered by @kalit the problem could be solved by adding "DOM" to "lib" property of tsconfig.json.
Generally speaking, the typeof operator in Typescript is useless because it does not allow you to be used with totally not defined variables, that goes in contrast with vanilla JS typeof operator.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.