
Build 10x products in minutes by chatting with AI - beyond just a prototype.
Topics
What is an empty object in TypeScript?
How to check if an object is empty in TypeScript?
How to make an empty object?
Is null or empty in TypeScript?
What does an empty object mean in TypeScript?
Some think itβs a completely blank structure, while others see it as an unrestricted type. This small detail can confuse, leading to unexpected errors in code.
The way TypeScript handles empty objects depends on type rules, object literals, and assignable values. Understanding these concepts helps avoid mistakes and write safer code.
This blog breaks it all down with simple examples, showing how a TypeScript empty object behaves and when to use it.
In TypeScript, an empty object can be represented by {}. At first glance, it might seem like it refers to an object with no properties, but TypeScript treats {} as a wildcard object type that can hold any properties.
const obj: {} = {}; // Valid: obj is an empty object const obj2: {} = { key: "value" }; // Also valid: {} allows any properties
An empty object in TypeScript is assignable to all object types but is not assignable to primitive types such as string, number, or boolean.
const obj: {} = 42; // β Error: Type 'number' is not assignable to type '{}' const obj2: {} = "text"; // β Error: Type 'string' is not assignable to type '{}' const obj3: {} = true; // β Error: Type 'boolean' is not assignable to type '{}'
An empty object type ({}) is different from an empty object (const obj = {}). The former is a type definition, while the latter is an instance.
It is an object type with no explicitly defined properties.
It allows any object that does not contain primitive types.
It is different from Record<string, never>, which strictly prevents properties.
type EmptyObjectType = {}; // Accepts any object const validObject: EmptyObjectType = { key: "value" }; // β Allowed type StrictEmptyObject = Record<string, never>; const invalidObject: StrictEmptyObject = { key: "value" }; // β Error: Property 'key' is not allowed
TypeScript follows structural typing, meaning that two object types are compatible if they have matching properties.
type MyObject = {}; const obj: MyObject = { name: "Alice", age: 30 }; // β Allowed (structural typing)
Since {} does not define any properties, it is assignable to any object.
By default, an empty object type {} does not allow null or undefined.
const obj: {} = null; // β Error: Type 'null' is not assignable to type '{}' const obj2: {} = undefined; // β Error: Type 'undefined' is not assignable to type '{}'
To allow these values, use a union type:
A generic function can use an empty object type as a type parameter to allow flexible usage.
Since is used, only object types are assignable, excluding primitive types.
Using ensures that a generic function only accepts valid objects.
β’ is stricter than object.
Use when you want to prevent any properties.
Prefer over in generic functions.
Avoid using unless explicitly required.
Ensure correct assignability to avoid unexpected errors.
The TypeScript empty object may seem simple, but it has unique behaviors. It accepts any object but does not work with primitive values like strings or numbers. Using in generic functions helps maintain type safety and prevents mistakes. A clear understanding of these rules makes coding smoother and avoids unexpected errors.
T extends {}T extends object{}Record<string, never>T extends objectT extends {}{}T extends objectconst obj: {} | null = null; // β
Allowed
const obj2: {} | undefined = undefined; // β
Allowedfunction processObject<T extends {}>(obj: T): T {
return obj;
}
const result = processObject({ name: "Alice" }); // β
Allowed
const result2 = processObject(42); // β Error: Type 'number' is not assignable to '{}'function validate<T extends object>(data: T): void {
console.log(data);
}
validate({ name: "Alice" }); // β
Allowed
validate("text"); // β Error: Type 'string' is not assignable to 'object'const obj: {} = 123; // β Error: Type 'number' is not assignable to type '{}'const obj: object = {}; // β
Allowed
const obj2: {} = 42; // β Errorconst obj: {} = undefined; // β Error: Type 'undefined' is not assignable to type '{}'