← Debugging Guides

阅读中文版本

TypeScript TS1208: Cannot find global type after adding an import

Fix TS1208 when a file with no imports becomes a module or when a global type declaration is missing from the compilation.

intermediatedebuggingtypescripttypes

Symptoms

  • error TS1208: Cannot find global type "Array" or similar
  • The error appears after exporting a value from a previously script-like file
  • A declaration file is not being picked up

Possible causes

  • The tsconfig lib or target is set too low for the used globals
  • A file that declared globals was converted to a module by adding export
  • A types package is missing from tsconfig types or node_modules
  • isolatedModules is on and a global declaration file is not referenced

Diagnosis steps

Read which global type is missing

Interpretation

A missing Array or Promise points at lib; a missing custom global points at a declaration file.

Check tsconfig lib and target

Interpretation

lib must include the ECMAScript version providing those globals.

Verify the declaring file is an ambient script

Interpretation

Adding export or import turns a global declaration into module scope, hiding the globals.

Root cause

TS1208 means a global type the code relies on is not visible in the compilation. Either the configured lib omits it, the declaration file that provided it became a module through an import or export, or a types package is excluded from the program.

Fix

Include the right lib and target in tsconfig, keep ambient declaration files free of top-level import or export statements (or move shared types to an explicit module and import them), and list required types packages in compilerOptions.types so they are loaded.

Verify the fix

Run tsc; the global resolves with no TS1208, and the declaration file remains part of the program without becoming a module.

Symptoms

error TS1208: Cannot find global type appears after a change to tsconfig or an added export. It blocks resolution of types the code clearly uses.

Diagnosis

Identify the missing global and the source of declarations:

{ "compilerOptions": { "target": "ES2020", "lib": ["ES2020", "DOM"], "types": [] } }
// globals.d.ts — must stay ambient (no import/export)
declare const BUILD_ID: string;

Fix

Set lib/target and keep declarations ambient:

{ "compilerOptions": { "lib": ["ES2022", "DOM"], "types": ["node"] } }

If the declaration needs imports, move it into a module and import the types explicitly.

Verification

Re-run tsc; TS1208 is gone and the global type resolves, confirming the declaration is back in program scope and the lib covers the used globals.

Ambient Files Are Global Only Until They Import

A declaration file stays global as long as it has no top-level import or export; adding one turns it into a module, and its declarations become module-scoped rather than visible everywhere. That single line is the usual cause of a suddenly missing global type. If the declaration genuinely needs to import something, move the exported types into a normal module and import them at each use, keeping truly ambient declarations import-free. Checking whether a declaration file is a script or a module is often faster than reading the whole config.