Skip to content

Type Error

Quick summary

You tried to use a value in a way that doesn't match its type — like calling `len()` on a number or adding a string to an integer.

Why this happens

  • Mixing incompatible types in an operation (e.g., int + str)

  • Calling a function with an argument of the wrong type

  • Accessing a property or method that doesn't exist on the value's type

Minimal example

✗ Broken code
age = 25
message = "I am " + age + " years old"
✓ Fixed code
age = 25
message = "I am " + str(age) + " years old"

Python cannot concatenate a string and an integer — convert age to a string first with str().

How to diagnose

  • Read the error message — it names the types and the operation involved

  • Check the types of the operands with typeof (JS) or type() (Python)

  • Trace where each value came from to find the type mismatch

How to fix

  • Convert the value to the correct type explicitly (e.g., str(age))

  • Fix the variable that holds the wrong type at its source

  • Use type guards or isinstance() checks before the operation

How to prevent

  • Use type annotations or TypeScript to catch type errors at compile time

  • Validate function inputs before using them

Related Resources

Related Lessons

Related Practice

← Back to language