Sub topic 7
Missing and unknown properties
This chapter assumes you can run npx tsc --noEmit, read the file, line, and error code of a diagnostic, and understand assignability at the level of "a value fits a type or it doesn't". It covers the two property errors you will see most often. One is a property the type requires but the value lacks. The other is a property the value uses or supplies that the type does not declare.
Two directions of the same problem
Both families come from comparing an object's shape with a type's shape. The direction of the mismatch decides which error you get.
| Situation | Typical code | Message starts with |
|---|---|---|
| Required property is absent from a value | TS2741 | Property 'x' is missing in type ... |
| Several required properties are absent | TS2739 | Type ... is missing the following properties from type ... |
| Code reads a property the type lacks | TS2339 | Property 'x' does not exist on type ... |
| Same, with a close spelling available | TS2551 | ... Did you mean 'y'? |
| Object literal has a property the target type lacks | TS2353 | Object literal may only specify known properties ... |
The codes are stable identifiers you can search for, but the message wording can change slightly between TypeScript versions.
Missing properties: TS2741 and TS2739
A missing property error means you are assigning a value to a type, and the value has fewer properties than the type requires.
interface User {
id: number;
name: string;
}
const user: User = { id: 1 };The compiler reports:
src/user.ts(6,7): error TS2741: Property 'name' is missing in type '{ id: number; }' but required in type 'User'.Read the message in two halves. The first half names what is missing (name). The second half names the value's actual type ({ id: number; }) and the type it was checked against (User). The location points at the variable being assigned, because that is where the mismatch is detected.
If two or more properties are missing, TypeScript switches to TS2739 and lists them:
error TS2739: Type '{ id: number; }' is missing the following properties from type 'User': name, emailChoosing a fix
There are four honest fixes. Pick by asking what is true about your program.
- The value is incomplete. Supply the property:
{ id: 1, name: "Ada" }. - The property is genuinely optional. Mark it in the type with
?. - This code only needs part of the type. Use a narrower type such as
Pick<User, "id">orPartial<User>. - The value is built in stages. Build the full object before assigning it to
User, or type the intermediate step asPartial<User>.
interface User {
id: number;
name: string;
nickname?: string;
}
type UserSummary = Pick<User, "id" | "name">;
const full: User = { id: 1, name: "Ada" };
const summary: UserSummary = { id: 2, name: "Alan" };
console.log(full.nickname ?? "none", summary.name);An optional property has the type string | undefined, so reading full.nickname gives you a value that may be undefined. That connects to the narrowing chapter. Making a property optional to silence TS2741 pushes the problem to every place that reads it, so use fix 2 only when absence is a real state of your data.
Unknown properties on a read: TS2339 and TS2551
TS2339 appears when you read or write a property the compiler cannot find on the type.
interface User {
id: number;
name: string;
}
function greet(user: User): string {
return `Hello, ${user.email}`;
}src/greet.ts(7,27): error TS2339: Property 'email' does not exist on type 'User'.When the name is close to a real property, the compiler adds a suggestion and uses TS2551 instead. For user.nmae you would see Property 'nmae' does not exist on type 'User'. Did you mean 'name'? Trust that suggestion first. Typos cause a large share of these errors.
If the spelling is right, decide which side is wrong:
- The type is out of date. Add the property to the interface, if the data really has it.
- The code assumes data the type never promised. Fix the code, or fetch the data from where it lives.
- The value comes from outside, such as JSON or an API. The declared type is then a claim about the data, so check that claim against the real source before widening it.
Properties that exist only on some union members
The most common surprise is a union. A property must exist on every member to be read without narrowing.
interface Circle {
kind: "circle";
radius: number;
}
interface Square {
kind: "square";
side: number;
}
type Shape = Circle | Square;
function area(shape: Shape): number {
return shape.radius ** 2 * Math.PI;
}error TS2339: Property 'radius' does not exist on type 'Shape'.
Property 'radius' does not exist on type 'Square'.The second line is the innermost cause. Shape could be a Square, and Square has no radius. The fix is to narrow first:
interface Circle {
kind: "circle";
radius: number;
}
interface Square {
kind: "square";
side: number;
}
type Shape = Circle | Square;
function area(shape: Shape): number {
if (shape.kind === "circle") {
return shape.radius ** 2 * Math.PI;
}
return shape.side ** 2;
}
console.log(area({ kind: "square", side: 3 }));Checking the kind field is the best narrowing tool here because it is a shared literal property. When the members have no such field, "radius" in shape also narrows.
Unknown properties on a write: TS2353
The third case runs the other way. You write an object literal with a property the target type does not declare.
interface User {
id: number;
name: string;
}
const user: User = { id: 1, name: "Ada", email: "[email protected]" };src/user.ts(6,43): error TS2353: Object literal may only specify known properties, and 'email' does not exist in type 'User'.This is called an excess property check. Older compiler versions reported the same message under TS2322, so search for the message text if your code differs. The check exists because a property you typed in a literal that the type ignores is nearly always a typo or a stale field name.
Excess property checks apply only to fresh literals
The check runs only when you write the object literal directly where it is checked. A value that already sits in a variable passes, because extra properties do not break assignability:
interface User {
id: number;
name: string;
}
const raw = { id: 1, name: "Ada", email: "[email protected]" };
const user: User = raw;
console.log(user.name);This compiles. The extra email is still on the object at runtime, but user.email is an error because User does not declare it. This is why the check is a typo detector and not a guarantee that objects have exactly the declared shape.
Fixing TS2353
- If the name is misspelled or stale, correct it.
- If the property belongs to the data, add it to the interface, usually as optional if not every producer sets it.
- If the type is meant to accept arbitrary extra keys, declare that with an index signature such as
[key: string]: unknown. This turns off the typo check for that type, so use it for real dictionaries, not as a shortcut.
A decision path
Edge cases worth knowing
- Optional chaining does not fix TS2339. Writing
user?.emailstill fails ifemailis not onUser. Optional chaining handlesundefinedvalues, not undeclared properties. exactOptionalPropertyTypes. With this option on, an optional propertynickname?: stringrejects an explicitnickname: undefined. This surfaces as a type mismatch on a property that looks present.noUncheckedIndexedAccess. Reading from an index signature then yieldsT | undefined, so dictionary reads can produce possibly-undefined errors even though the key is "known".- Casting to silence TS2741.
{ id: 1 } as Usercompiles, anduser.nameisundefinedat runtime while the type saysstring. Assertions are covered in a later chapter, and they are the wrong first move here.
Exercise
Given interface Point { x: number; y: number }, which error does const p: Point = { x: 1, y: 2, z: 3 }; produce, which does const q: Point = { x: 1 }; produce, and what changes if you first write const raw = { x: 1, y: 2, z: 3 }; and then const r: Point = raw;?
Show answer
The first line gives TS2353 (Object literal may only specify known properties, and 'z' does not exist in type 'Point'), because the literal is fresh. The second gives TS2741 (Property 'y' is missing in type '{ x: number; }' but required in type 'Point'). The raw version compiles with no error, because excess property checks apply only to fresh object literals, so z is allowed through the variable. Reading r.z would still fail with TS2339.
Exercise
This function fails with TS2339 on item.price. What are two different correct fixes, depending on the cause? type Item = { kind: "book"; pages: number } | { kind: "toy"; price: number }; function cost(item: Item) { return item.price; }
Show answer
The cause is that price exists only on the toy member of the union. If the function should handle both kinds, narrow first with if (item.kind === "toy") { return item.price; } and return something sensible for the book case. If books should also have a price, add price: number to the book member of the type. Casting item to the toy type would compile, but it would give undefined at runtime for books.