EMZETT.
Login

TypeError (Python and JavaScript)

In short: A value does not have the type the operation requires, such as text plus a number or calling something that is not a function.

In more detail: A TypeError arises when an operation or function receives a value of the wrong type. The message usually says which type was expected and what was actually present.

In Depth

Typical messages

Python

  • TypeError: can only concatenate str (not "int") to str: text and number joined with +.
  • TypeError: 'NoneType' object is not subscriptable: x[0] applied to None.
  • TypeError: 'int' object is not callable: a variable overwrote a function name.
  • TypeError: f() takes 1 positional argument but 2 were given.

JavaScript

  • TypeError: x is not a function: calling something that is not a function.
  • TypeError: Cannot read properties of undefined (reading 'name'): access on undefined.
  • TypeError: Assignment to constant variable: reassigning a const variable.

Causes

  • Missing data (None/undefined/null) because a search found nothing or a response is absent.
  • Wrong data type from inputs (strings instead of numbers, for example with input() or forms).
  • Wrong number or order of arguments.
  • Variable names shadow functions (list = [1,2], later list(x)).

Solutions

  1. Check the type: print(type(x)) or console.log(typeof x).
  2. Convert: int(text), str(number), f-strings (f"{a} and {b}"), template literals in JS.
  3. Check for None/undefined before accessing (if x is not None, x?.name in JS).
  4. Look at the error message’s line number and print the value before it.
  5. Do not use function names for variables. In TypeScript the compiler catches many of these errors early.

See also: Cannot read properties of undefined, ModuleNotFoundError, NullPointerException