TypeScript Reference ยท Error Codes Worth Knowing
TypeScript error TS2339: Property does not exist on type
Reference for TypeScript error TS2339: why a property is reported as missing, how it differs from TS2551 suggestions, and how to fix unions, DOM and JSON cases.
TS2339 means you read or wrote a property that the compiler cannot find on the type of the expression. The full message is Property 'x' does not exist on type 'T'. It protects you from typos, from using a field before you check which variant of a union you have, and from assuming a value has more structure than its declaration says.
When the name you typed is close to a real property, TypeScript reports TS2551 instead ("Did you mean ...?"). The cause and the fixes are the same.
const user = { name: "Ann" }; user.email; // TS2339
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Syntax
The error is produced by property access, a.b, on any expression.
| Situation | Typical fix |
|---|---|
| The property is misspelled | Correct the name (TS2551 suggests one). |
| The property exists only on some union members | Narrow with kind, in, typeof or instanceof. |
| The type is incomplete | Add the property to the interface or type alias. |
| The object is a dictionary | Use an index signature or Record<string, T>. |
| A DOM element is too generic | Narrow with instanceof HTMLInputElement or a typed query. |
| A global is missing | Declare it with declare or augment the global interface. |
JSON or unknown data |
Validate it, then assign it to a declared type. |
Edge cases: on the type {} or object, no properties are known. Writing a new property to an object literal variable is also TS2339; declare it up front or use a mapped or index type.
Usage notes
- Check the type, not just the name. Hover the expression in the editor: the type is often narrower or wider than you assumed, for example
Element | nullfromquerySelector. - Unions need narrowing. Accessing a property is allowed only when every member has it; the
inoperator and discriminant fields are the idiomatic checks. - Optional chaining (
a?.b) deals withnullorundefined(TS2531), but does not create properties that are missing from the type. - Do not reach for
any.(obj as any).emailcompiles but removes all checking for that value; a declared optional property is better. - Declaration merging lets you add members to an existing interface (for example
Window) in a.d.tsfile or adeclare globalblock. - Dynamic keys: with
obj[key]the error is TS7053, not this one.
Related
- TS2322
- TS2531
- TS7053
- Tutorial: Narrowing, Discriminated unions