TS2454: Variable is used before being assigned TypeScript Origin: On your device OSI layer 7 · Application Last updated 5 Nov 2026

TypeScript: error TS2454: Variable 'x' is used before being assigned.

Control flow shows a path where the variable is read before any assignment.

Exact message you may see error TS2454: Variable 'x' is used before being assigned.Variable 'x' is used before being assigned. ts(2454)TS2454 Variable is used before being assignederror TS2454
Seen on: TypeScript

Meaning

Initialise at declaration (let x: number | undefined; or a default), assign in every branch, or restructure with const x = cond ? a : b. A definite assignment assertion (let x!: number) silences it if you guarantee assignment elsewhere (e.g., in callbacks).

Common causes

  • Assigned only inside if/loops/try
  • Assigned in a callback (TS can't see)
  • switch without default
  • Early use before assignment
  • Class property without initialiser

⚡ Quick fix

  1. const x = cond ? 1 : 2;
  2. Initialise: let x: T | undefined; then check
  3. Definite assignment let x!: T if guaranteed

Detailed fix by platform

TypeScript

  1. let timer: ReturnType<typeof setTimeout> | undefined;

How to diagnose

  1. Path — Unassigned read?
  2. Callback — Assigned inside?
  3. Assertion — Safe?

🔧 Still not fixed?

Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:

Where this error sits

Origin: On your device. Errors that are often confused with or caused by this one (click to open):

Cannot find module './App.vue' or its corresponding type declarations — TypeScript: error TS2307: Cannot find module './App.vue' or its corresponding type declarations. (Vue)Cannot find module '.…NG0955 — Angular: NG0955: The provided track expression resulted in duplicated keys for a given collection (@for) (Angular)NG0955TS1005: ';' expected / ',' expected (syntax error) — TypeScript: error TS1005: ';' expected. (TypeScript)TS1005: ';' expected …TS1128: Declaration or statement expected — TypeScript: error TS1128: Declaration or statement expected. (TypeScript)TS1128: Declaration o…TS1192: Module has no default export — TypeScript: error TS1192: Module '"./utils"' has no default export. (TypeScript)TS1192: Module has no…TS1208: File cannot be compiled under '--isolatedModules' because it is considered a global script file — TypeScript: error TS1208: 'index.ts' cannot be compiled under '--isolatedModules' because it is considered a global script file. Add an import, export, or an empty 'export {}' statement (TypeScript)TS1208: File cannot b…TS18048: 'x' is possibly 'undefined' — TypeScript: error TS18048: 'user' is possibly 'undefined'. (TypeScript)TS18048: 'x' is possi…TS2366: Function lacks ending return statement — TypeScript: error TS2366: Function lacks ending return statement and return type does not include 'undefined'. (TypeScript)TS2366: Function lack…TS2454: V…

Explore the map of this error Full error map →

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.

💬 What worked for others

No tips yet — did something else fix this for you?

Share what worked for you

Tips are checked by an editor before they appear. Links are removed.