Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript cannot find name window or document

For either case:

document.getElementById('body'); // or window.document.getElementById('body'); 

I get error TS2304: Cannot find name 'window'.

Am I missing something in tsconfig.json for a definition file I should install?

I get the message when running tsc and in vscode

tsconfig.json:

{     "compilerOptions": {         "allowJs": true,         "emitDecoratorMetadata": true,         "experimentalDecorators": true,         "jsx": "react",         "module": "commonjs",         "moduleResolution": "node",         "noEmitOnError": true,         "noImplicitAny": false,         "sourceMap": true,         "suppressImplicitAnyIndexErrors": true,         "target": "ES2016",         "typeRoots": [             "node_modules/@types/",             "typings/index.d.ts"         ]     },     "exclude": [         "node_modules",         "**/*-aot.ts"     ] } 

My Answer: For use with tsconfig.json I target es5 and use lib: ["es2015", "dom"]

like image 687
Steven Bayer Avatar asked Dec 26 '16 21:12

Steven Bayer


People also ask

How to fix cannot find name in TypeScript?

To solve the error "Cannot find name module", install the node types by running npm i -D @types/node . If the error is not resolved, try adding node to your types array in tsconfig. json and restarting your IDE.


1 Answers

use

"lib": ["dom"] 

in tsconfig.json

e.g.

{   "compilerOptions": {     "lib": ["es5", "es6", "dom"],     "outDir": "./dist/",     "sourceMap": true,     "noImplicitAny": true,     "module": "commonjs",     "target": "es6",     "moduleResolution": "node",     "jsx": "react"   },   "include": ["./src/**/*"] } 
like image 181
Damian Green Avatar answered Sep 27 '22 21:09

Damian Green