TS2532
TypeScript Programming Language
Severity: MinorWhat Does This Error Mean?
TypeScript error TS2532 means you are accessing a property or calling a method on a value that TypeScript knows might be undefined. If it is undefined at runtime, your code will crash with 'Cannot read properties of undefined'. TypeScript is catching this before it happens. The fix is to check for undefined before using the value, use optional chaining, or provide a fallback with the nullish coalescing operator.
Affected Models
- TypeScript 2.0 and later
- All TypeScript versions with strictNullChecks enabled
Common Causes
- Accessing an array element by index without checking whether the index exists (array[i] can be undefined if i is out of bounds)
- Using a function parameter that has a default value or is marked as optional (its type includes | undefined)
- Accessing an optional object property marked with ? in its type definition
- A function that can return undefined being used without checking the result
- A Map.get() or array.find() call whose result is undefined when no matching item is found
How to Fix It
-
Add an undefined check before using the value. Use: if (myValue !== undefined) { myValue.doSomething(); }. TypeScript narrows the type inside the if block and no longer considers it undefined.
You can also use if (myValue) as a shorter check, but be aware this also catches falsy values like 0, empty string, and false — not just undefined.
-
Use optional chaining (?.) to safely access properties on a possibly-undefined value. Example: user?.address?.city returns undefined if user or address is undefined, instead of crashing.
Optional chaining short-circuits — as soon as it hits undefined, it stops and returns undefined without trying to go further.
-
For array element access, check the array length or check the result before using it. Example: const item = myArray[index]; if (item !== undefined) { use(item); }.
TypeScript with noUncheckedIndexedAccess enabled makes array element access return T | undefined. This catches a common real-world bug.
-
For Map.get() and array.find(), always handle the undefined case. Example: const found = myMap.get(key); if (found !== undefined) { use(found); } — because get() returns undefined when the key does not exist.
Map.get() and find() are typed to return T | undefined because they genuinely can find nothing. TypeScript is accurately reflecting this.
-
Provide a default value using the nullish coalescing operator. Example: const label = user?.name ?? 'Guest'. If name is undefined, label becomes 'Guest' automatically.
This pattern is clean for optional display values where a fallback makes sense. It handles the undefined case inline without an if statement.
When to Call a Professional
TS2532 is a compile-time type safety error. No outside help is needed. Handling undefined correctly makes your application more reliable and prevents real crashes for your users.
Frequently Asked Questions
What is the difference between TS2531 (possibly null) and TS2532 (possibly undefined)?
TS2531 fires when the type is T | null — the value could be explicitly null. TS2532 fires when the type is T | undefined — the value could be undefined. They are separate because null and undefined have different meanings in JavaScript. null usually means 'intentionally empty', while undefined usually means 'never set' or 'does not exist'.
Why does TypeScript flag array[i] as possibly undefined but not always?
TypeScript only adds undefined to array element access when the noUncheckedIndexedAccess compiler option is enabled. With the standard settings, TypeScript assumes your array index is always valid — which is not always true. Enabling noUncheckedIndexedAccess makes TypeScript stricter and catches more real bugs.
My function parameter is typed as string | undefined. How do I use it without TS2532?
Either check for undefined first: if (param !== undefined) { use(param); }, or provide a default value using parameter defaults: function myFn(param: string = 'default') {}. Using a default value in the function signature tells TypeScript the parameter is always a string inside the function body.