TypeScript Reference ยท Primitive and Special Types
TypeScript Object and {} types
Understand the capital-O Object type and the empty type {} in TypeScript: they accept nearly every value except null and undefined, so use them with care.
Object (capital O) and {} are two types that surprise newcomers. Both mean "any value that is not null or undefined". That includes strings, numbers, booleans and symbols, because primitives can use members from Object.prototype such as toString and valueOf.
So neither type means "an object" in the everyday sense. For that, the lowercase object type is the right choice. Object and {} are mostly seen in old code, in generic constraints, and in lint rules that tell you to stop using them.
let a: {} = "text"; // allowed let b: Object = 42; // allowed let c: object = {}; // fine // let d: {} = null; // error under strictNullChecks
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.
Syntax
let x: Object; let y: {}; function f<T extends {}>(v: T): T { return v; }
| Type | Accepts | Rejects |
|---|---|---|
{} |
Every value except null and undefined. |
null, undefined (with strictNullChecks). |
Object |
Every value except null and undefined, typed with the members of Object.prototype. |
null, undefined (with strictNullChecks). |
object |
Only non-primitive values. | All primitives, null, undefined. |
Edge cases:
- With
strictNullChecksoff,nullandundefinedare assignable to both. - A value of type
{}has no known properties, so you cannot readvalue.namewithout narrowing first. - An object literal type with members, such as
{ id: number }, is a different, much more useful type; only the empty literal{}is special.
Usage notes
- Avoid them as annotations. They hide mistakes:
let x: {} = "hello"compiles but the name suggests an object. Useobject,unknown,Record<string, unknown>or a real interface. {}in generics.T extends {}means "any non-nullish value". Before TypeScript 4.8 the unconstrainedTmeant{}-ish; today an unconstrainedTincludesnullandundefined.NonNullable<T>. This utility is defined asT & {}, which removesnullandundefinedfrom a union.- Intersections.
{}is the identity for object intersections:A & {}isAfor non-nullishA. This is used in tricks that keep literal unions from collapsing, such as"a" | "b" | (string & {}). - Lint rules.
@typescript-eslint/no-empty-object-typeandno-wrapper-object-typesflag these types for good reasons. - Prefer
unknownfor "anything".unknownalso coversnullandundefined, and forces you to narrow before use.