typescript type_error ai_generated true

error TS2304: Cannot find name 'identifier'

ID: typescript/ts2304-cannot-find-name

Also available as: JSON · Markdown
90%Fix Rate
92%Confidence
50Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
5 active

Root Cause

TypeScript can't resolve a name. Missing import, missing type declaration, or wrong tsconfig.

generic

Workarounds

  1. 95% success Add the missing import or install @types package
    npm install -D @types/package-name

    Sources: https://www.typescriptlang.org/docs/handbook/2/type-declarations.html

  2. 85% success Check tsconfig include/exclude paths and module resolution settings
    Check tsconfig include/exclude paths and module resolution settings

    Sources: https://www.typescriptlang.org/tsconfig#include

  3. 88% success For global types, add a .d.ts file and include it in tsconfig
    // Create src/global.d.ts:
    declare const __DEV__: boolean;
    declare const API_URL: string;
    
    // In tsconfig.json:
    { "include": ["src/**/*.ts", "src/**/*.d.ts"] }

    Sources: https://www.typescriptlang.org/docs/handbook/2/type-declarations.html

Dead Ends

Common approaches that don't work:

  1. Add // @ts-ignore above the line 85% fail

    Disables all type checking for that line, hiding real errors

  2. Cast to any 80% fail

    Defeats the purpose of TypeScript

Error Chain

Leads to:
Preceded by:
Frequently confused with: