error TS2318: Cannot find global type 'Object'

Austin France picture Austin France · Jun 6, 2018 · Viewed 10.4k times · Source

Very simple project, no tsconfig.json just using the command line:

tsc --jsx react --target es6 --lib dom src/lib/Fetch.tsx

and I get the following errors and I don't understand why, these types are literally defined everywhere, and specifically in lib.es6.d.ts which is I believe included by --target es6

error TS2318: Cannot find global type 'Array'.
error TS2318: Cannot find global type 'Boolean'.
error TS2318: Cannot find global type 'Function'.
error TS2318: Cannot find global type 'IArguments'.
error TS2318: Cannot find global type 'Number'.
error TS2318: Cannot find global type 'Object'.
error TS2318: Cannot find global type 'RegExp'.

Have the following dependencies installed

"dependencies": {
  "@types/react-dom": "^16.0.6",
  "@types/react": "^16.3.16",
  "react": "^16.4.0",
  "react-dom": "^16.4.0"
},
"devDependencies": {
  "@types/node": "^10.3.1",
  "typescript": "^2.9.1"
}

RESOLVED

Figured out my problem. Having typescript installed locally, but was running the globally installed and older version typescript. When I ran tsc via npm scripts it compiles with the following command:

tsc --jsx react --target es6 --module commonjs src/lib/Fetch.tsx

Answer

Titian Cernicova-Dragomir picture Titian Cernicova-Dragomir · Jun 6, 2018

dom contains just the definitions for browser specific DOM elements. You need to include one of the es* libraries which includes the core es types needed for compilation. Once you specify a --lib option you have to include all necessary types, the compiler will no longer include any library based on your target:

tsc --jsx react --target es6 --lib es2015,dom src/lib/Fetch.tsx

Or if you are not actually just using the default libraries, you can just omit the option altogether:

tsc --jsx react --target es6 src/lib/Fetch.tsx