Design Converter
Education
Last updated on Mar 3, 2025
•5 mins read
Last updated on Mar 3, 2025
•5 mins read
Software Development Executive - II
I know who I am.
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.
1const obj: {} = {}; // Valid: obj is an empty object 2const 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.
1const obj: {} = 42; // ❌ Error: Type 'number' is not assignable to type '{}' 2const obj2: {} = "text"; // ❌ Error: Type 'string' is not assignable to type '{}' 3const 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.
1type EmptyObjectType = {}; // Accepts any object 2const validObject: EmptyObjectType = { key: "value" }; // ✅ Allowed 3 4type StrictEmptyObject = Record<string, never>; 5const 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.
1type MyObject = {}; 2const 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.
1const obj: {} = null; // ❌ Error: Type 'null' is not assignable to type '{}' 2const obj2: {} = undefined; // ❌ Error: Type 'undefined' is not assignable to type '{}'
To allow these values, use a union type:
1const obj: {} | null = null; // ✅ Allowed 2const obj2: {} | undefined = undefined; // ✅ Allowed
A generic function can use an empty object type as a type parameter to allow flexible usage.
1function processObject<T extends {}>(obj: T): T { 2 return obj; 3} 4 5const result = processObject({ name: "Alice" }); // ✅ Allowed 6const result2 = processObject(42); // ❌ Error: Type 'number' is not assignable to '{}'
Since T extends {}
is used, only object types are assignable, excluding primitive types.
Using T extends object
ensures that a generic function only accepts valid objects.
1function validate<T extends object>(data: T): void { 2 console.log(data); 3} 4 5validate({ name: "Alice" }); // ✅ Allowed 6validate("text"); // ❌ Error: Type 'string' is not assignable to 'object'
1const obj: {} = 123; // ❌ Error: Type 'number' is not assignable to type '{}'
1const obj: object = {}; // ✅ Allowed 2const obj2: {} = 42; // ❌ Error
• {}
is stricter than object.
1const obj: {} = undefined; // ❌ Error: Type 'undefined' is not assignable to type '{}'
Use Record<string, never>
when you want to prevent any properties.
Prefer T extends object
over T extends {}
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 T extends object
in generic functions helps maintain type safety and prevents mistakes. A clear understanding of these rules makes coding smoother and avoids unexpected errors.
Tired of manually designing screens, coding on weekends, and technical debt? Let DhiWise handle it for you!
You can build an e-commerce store, healthcare app, portfolio, blogging website, social media or admin panel right away. Use our library of 40+ pre-built free templates to create your first application using DhiWise.