Sub topic 7
Missing and unknown properties
This chapter assumes you know basic JavaScript objects and TypeScript interfaces, type annotations, and union types. The goal is to read whether an object has too little, too much, or incorrectly accessed data.
Missing required properties
A required property is a property that a type says must exist. If an object does not provide it, TypeScript reports an error such as TS2741.
interface User {
id: number;
name: string;
}
const user: User = { id: 1 };The diagnostic says that name is missing but required in User. The annotation : User asks TypeScript to check the entire object against that interface.
Add the property when the data should contain it:
interface User {
id: number;
name: string;
}
const user: User = {
id: 1,
name: "Ada",
};If the property is genuinely optional, mark it with ? instead:
interface User {
id: number;
name?: string;
}
const user: User = { id: 1 };An optional property has the type string | undefined when strict null checks are enabled. Code that uses it may need to check for undefined.
Unknown properties
TypeScript reports TS2339 when you read a property that the value's type does not declare.
interface User {
id: number;
name: string;
}
function greet(user: User): string {
return `Hello, ${user.email}`;
}email is unknown on User. Fix the code if the access is a mistake:
function greet(user: User): string {
return `Hello, ${user.name}`;
}Otherwise, add the property to the type and provide it in the data:
interface User {
id: number;
name: string;
email: string;
}
function greet(user: User): string {
return `Contact ${user.email}`;
}Do not solve every unknown-property error by adding an index signature such as [key: string]: unknown. An index signature allows arbitrary keys and can hide misspellings. Use it only when the data format really permits unknown keys.
Extra properties in object literals
When an object literal is checked directly against a type, TypeScript also performs an excess-property check.
interface User {
id: number;
name: string;
}
const user: User = {
id: 1,
name: "Ada",
role: "admin",
};The role property is not declared in User, so TypeScript reports an error, often TS2353. Remove role, add it to the interface, or use a separate type that describes administrative users.
A variable can behave differently because TypeScript first infers its type:
const data = {
id: 1,
name: "Ada",
role: "admin",
};
const user: User = data;This can be assignable because data contains all required User properties. The extra property remains in data, even though it is not available through the user variable's User type. Treat this as a reminder to model the intended boundary clearly.
:::callout note Fix the type at the boundary where the data enters your program. Avoid optional properties or index signatures merely to make an error disappear. :::
| Situation | Typical diagnostic | Usual fix |
|---|---|---|
| Required property is absent | TS2741 | Add it or make it optional |
| Accessed property is undeclared | TS2339 | Correct the access or update the type |
| Object literal has an extra property | TS2353 | Remove or declare the property |
Exercise
An interface has required properties id and name, but an object has only id. What should you do?
Show answer
Add name, or mark name optional only if the application can correctly handle a missing name.