← Home ← Codex ← DEBT
Browse by Category
+ added · updated 7d
← Back to glossary

TypeError — Common Causes and Fixes

JavaScript ES5 Beginner
debt(d5/e3/b3/t7)
d5 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'specialist tool catches it' (d5). The detection_hints list TypeScript and ESLint as the tools that catch these issues. Without TypeScript strict mode enabled, plain JavaScript lets null/undefined property access sail past silently until runtime. ESLint can flag some patterns but not all. The common case isn't caught by default linting alone — you need TypeScript's strict null checks or deliberate ESLint rules targeting optional chaining, placing this firmly at d5.

e3 Effort Remediation debt — work required to fix once spotted

Closest to 'simple parameterised fix' (e3). The quick_fix states enabling TypeScript strict mode or using optional chaining (?.) on external data property accesses. This is more than a single-line patch because it requires identifying and updating all call sites that access potentially-null values from API responses or async data, but it's still a localised pattern-replacement rather than a cross-cutting architectural change. e3 fits.

b3 Burden Structural debt — long-term weight of choosing wrong

Closest to 'localised tax' (b3). The applies_to covers both web and cli contexts broadly, but the burden is more a recurring development discipline (always use optional chaining on external data) rather than a deep structural weight. It doesn't shape the entire architecture — it's a per-component tax on every place external/async data is consumed. Slightly above b1 because it affects multiple files and requires ongoing vigilance, but doesn't reach b5's persistent productivity tax level.

t7 Trap Cognitive debt — how counter-intuitive correct behaviour is

Closest to 'serious trap' (t7). The misconception field directly states that TypeErrors frequently occur with correct code receiving unexpected API responses or async race conditions — not just bad code. This directly contradicts the intuition of many competent developers who assume TypeErrors indicate a coding mistake, not a data/timing issue. The async data availability mistake also contradicts synchronous mental models. This is a documented cross-ecosystem gotcha that trips up developers familiar with other languages, earning t7.

About DEBT scoring →

Also Known As

Cannot read properties of undefined is not a function null is not an object

TL;DR

TypeError is thrown when a value is not of the expected type — most commonly 'Cannot read properties of undefined/null', the most frequent JavaScript runtime error.

Explanation

TypeError occurs when an operation is performed on an incompatible type. The most common: accessing a property on undefined or null. This happens when API responses have unexpected shapes, optional chaining is missing, or async data has not loaded yet. Other common TypeErrors: calling something that is not a function, accessing .length on null, passing wrong argument types. TypeScript catches most TypeErrors at compile time. In PHP, similar errors manifest as 'Call to a member function on null'.

Common Misconception

TypeErrors only happen with bad code — they frequently happen with correct code that receives unexpected API responses or race conditions in async loading.

Why It Matters

TypeError from null/undefined is the most common JavaScript runtime error in production — optional chaining (?.) and nullish coalescing (??) prevent the majority of them.

Common Mistakes

  • Not using optional chaining for potentially-null API response properties
  • Forgetting async data is not available synchronously
  • Not checking Array.isArray() before calling array methods on unknown values

Code Examples

✗ Vulnerable
const user = null;
console.log(user.name); // TypeError: Cannot read properties of null

const num = 42;
num.toUpperCase(); // TypeError: num.toUpperCase is not a function
✓ Fixed
// Optional chaining:
console.log(user?.name); // undefined, no throw

// Type check before calling:
if (typeof value === 'string') {
    value.toUpperCase();
}

// TypeScript catches these at compile time

Added 22 Mar 2026
Edited 12 Jun 2026
Views 45
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings T 0 pings W 1 ping T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 2 pings F 1 ping S 3 pings S 2 pings M 1 ping T 0 pings W 0 pings T 1 ping F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 2 pings T 0 pings W
No pings yet today
Bing 1 SEMrush 1
Scrapy 8 Amazonbot 7 Ahrefs 4 Perplexity 3 Unknown AI 2 Claude 2 Majestic 1 Google 1 Meta AI 1 Qwen 1 Bing 1 SEMrush 1
crawler 30 crawler_json 2
DEV INTEL Tools & Severity
🟠 High ⚙ Fix effort: Low
⚡ Quick Fix
Enable TypeScript strict mode or use optional chaining (?.) on all property accesses from external data — catches the majority of TypeError sources at development time
📦 Applies To
javascript ES5 web cli
🔗 Prerequisites
🔍 Detection Hints
obj.prop without null check; array.method() without Array.isArray(); response.data.field without optional chaining; no TypeScript strict mode
Auto-detectable: ✓ Yes typescript eslint
⚠ Related Problems
🤖 AI Agent
Confidence: High False Positives: Medium ✗ Manual fix Fix: Low Context: Function Tests: Update
CWE-704

✓ schema.org compliant