TypeError
What is TypeError?
TypeError: An operation is performed on a value of the wrong type — like calling a number as if it were a function. A TypeError means an operation got a value of the wrong type. Calling something that is not a function, reading a property of undefined, or passing a string where a PHP function declares an int all produce one.
Frequently asked questions about TypeError
How is TypeError used in practice?
In JavaScript, Cannot read properties of undefined is the most common example, usually because data was not loaded yet or a field was missing. In PHP with strict types, a TypeError tells you exactly which argument had the wrong type.
What should you keep in mind with TypeError?
Type declarations and static analysis catch most of these before the code runs. Handle optional data explicitly, with optional chaining like user?.address in JavaScript or the null-safe operator in PHP. When a TypeError happens with data from an API or a form, validate that data at the point it enters your system.
At a glance
| Category | Error types |
|---|---|
| Explained by | Reinier de la Parra |
| Updated |