TypeError: What It Means and How to Fix It
Error reference
TypeError
Language
Severity
HighWhen it happens
A TypeError is raised when an operation is applied to a value of the wrong type.
Potential fixes
Convert values to the correct type before using them (e.g. int(), str() in Python). In JavaScript, guard against null and undefined with optional chaining (?.) or explicit checks before calling methods.
Related errors
Deep dive
A TypeError is raised when an operation is applied to a value of the wrong type. In Python it commonly appears when mixing incompatible types in an expression. In JavaScript it appears when you try to call a method on null, undefined, or a non-function.
Why it happens
- •Mixing strings and numbers in arithmetic without explicit conversion (Python).
- •Calling a method on null or undefined (JavaScript).
- •Passing the wrong number of arguments to a function.
- •Treating a non-iterable value as if it were iterable.
Potential fixes
Convert values to the correct type before using them (e.g. int(), str() in Python). In JavaScript, guard against null and undefined with optional chaining (?.) or explicit checks before calling methods.
Examples
Code that triggers the error
age = "25"
result = age + 5
print(result)Error output
Traceback (most recent call last):
File "app.py", line 2, in <module>
result = age + 5
TypeError: can only concatenate str (not "int") to strFixed code
age = "25"
result = int(age) + 5
print(result) # 30Code that triggers the error
const value = null;
console.log(value.toUpperCase());Error output
TypeError: Cannot read properties of null (reading 'toUpperCase')
Fixed code
const value = null;
if (value !== null && value !== undefined) {
console.log(value.toUpperCase());
}
// Or with optional chaining:
console.log(value?.toUpperCase());Practice in English
How would you explain a TypeError to a fellow dev? Choose the right phrase:
"I hit an error...
Ready to practice your English at work?
Lingua-e has interactive exercises built around real developer conversations: standups, code reviews, retrospectives, and more. Practice until it comes naturally.
Try Lingua-e for free