Skip to content
jshacks.dev
TypeScript

object vs Object vs {} in TypeScript

Three types that all sound like any object. Learn what object, Object and {} each accept, how they differ for primitives and null, and which one to pick.

7 min read
Summarize in AI
Flat illustration of two options weighed side by side, with the TypeScript, Vite, JavaScript, npm and Node.js logos.

TypeScript has three types that look like they mean "an object": lowercase object, capitalised Object, and the empty type literal {}. They are not synonyms, and the differences are not about style. One of them accepts numbers and strings. One rejects them. One carries a set of members that your values must be compatible with. Picking the wrong one produces either a type that accepts far too much or a type that rejects code you expected to work. This post explains what each of the three means, the exact errors you get, how null and undefined fit in, and what to write instead in most real code. It describes TypeScript 6.x with strict on, and the native compiler preview that TypeScript 7 is built on follows the same rules. The reference is the handbook section on the object type and the page on type compatibility.

object: any non-primitive

The lowercase object type means any value that is not a primitive. Objects, arrays, functions, class instances and Map instances are all accepted. Numbers, strings, booleans, symbols, bigints, null and undefined are not.

lower.tsTypeScript
const a: object = { x: 1 };
const b: object = [1, 2, 3];
const c: object = () => 42;
const d: object = new Map<string, number>();

const e: object = 1;
const f: object = "text";
const g: object = null;
Text
lower.ts(6,7): error TS2322: Type 'number' is not assignable to type 'object'.
lower.ts(7,7): error TS2322: Type 'string' is not assignable to type 'object'.
lower.ts(8,7): error TS2322: Type 'null' is not assignable to type 'object'.

The first four lines compile, and the last three each produce one error. The type matches the JavaScript notion of a value you can attach properties to, and it is what you want when a function should accept "something that is not a primitive".

You cannot do much with a value of type object, because the type says nothing about its shape.

lower.tsTypeScript
function inspect(o: object): void {
  console.log(o.name);
}
Text
lower.ts(2,15): error TS2339: Property 'name' does not exist on type 'object'.

The type tells you the value is an object and nothing else, so any property read is an error until you narrow. That makes object a good type for "I only need to know this is not a primitive", for example a WeakMap key or a Object.keys argument, and a poor one for describing structure.

{}: anything except null and undefined

The empty type literal {} describes a type with no required properties. Since every non-nullish value has no required properties relative to it, {} accepts every value except null and undefined, including primitives.

empty.tsTypeScript
const a: {} = { x: 1 };
const b: {} = 1;
const c: {} = "text";
const d: {} = true;

const e: {} = null;
const f: {} = undefined;
Text
empty.ts(6,7): error TS2322: Type 'null' is not assignable to type '{}'.
empty.ts(7,7): error TS2322: Type 'undefined' is not assignable to type '{}'.

Many people expect {} to mean "an empty object", and it does not. A value of type {} can be the number 1. The type means "no constraints on shape", and the only values it refuses are the two that have no properties at all, which only applies under strictNullChecks. With that option off, null and undefined are assignable to everything, and the last two lines compile.

This meaning is the reason {} shows up inside the standard utility types. NonNullable<T> is defined as T & {}. Intersecting with {} removes null and undefined from a type and leaves everything else alone, which is exactly the filter you want.

nonnull.tsTypeScript
type Clean = NonNullable<string | number | null | undefined>;

const ok: Clean = "a";
const bad: Clean = null;
Text
nonnull.ts(4,7): error TS2322: Type 'null' is not assignable to type 'string | number'.

The error shows the resulting type, string | number. This is the one legitimate use of {} in modern code.

Object: the interface with the built-in members

The capitalised Object is not a keyword. It is the name of the global interface that describes the members every JavaScript value inherits, such as toString, valueOf and hasOwnProperty. Because primitives are treated as having those members through their wrapper objects, Object accepts primitives as well, just like {}.

upper.tsTypeScript
const a: Object = { x: 1 };
const b: Object = 1;
const c: Object = "text";

const d: Object = null;
Text
upper.ts(5,7): error TS2322: Type 'null' is not assignable to type 'Object'.

So far Object and {} behave identically. The difference appears when a value has a member with the same name as one on Object but a different type.

upper.tsTypeScript
const bad: Object = {
  toString(): number {
    return 1;
  },
};

const fine: {} = {
  toString(): number {
    return 1;
  },
};
Text
upper.ts(1,7): error TS2322: Type '{ toString(): number; }' is not assignable to type 'Object'.
  Types of property 'toString' are incompatible.
    Type '() => number' is not assignable to type '() => string'.
      Type 'number' is not assignable to type 'string'.

The first object is rejected, because Object requires toString to return a string, and the second is accepted, because {} requires nothing. This is the only practical difference between the two, and it is a small one. It also means Object quietly validates the signatures of those inherited methods, which is rarely what the author intended.

On top of that, Object is easy to confuse with the Object constructor value, and with the other wrapper types String, Number, Boolean, Symbol and BigInt. The capitalised wrapper types describe the object forms made by new String("x"), which almost nobody uses, and primitives are assignable to them only because of the apparent-members rule. The handbook advises against all of them, and lint rules such as the typescript-eslint rules named no-wrapper-object-types and no-empty-object-type flag them for that reason.

A side by side check

It helps to see all three against the same values.

compare.tsTypeScript
const values = {
  number: 1,
  array: [1],
  nul: null,
};

const o1: object = values.number;
const o2: {} = values.number;
const o3: Object = values.number;

const p1: object = values.array;
const p2: {} = values.array;
const p3: Object = values.array;
Text
compare.ts(7,7): error TS2322: Type 'number' is not assignable to type 'object'.

Only one line fails. The number is rejected by object and accepted by {} and Object, and the array is accepted by all three. If a function must refuse primitives, object is the only one of the three that does so.

What to write instead

In most code, none of the three is the best answer, because each says too little about the value. There are better choices, depending on what you mean.

If you mean "a dictionary of unknown values", use Record<string, unknown>. That type has string keys and unknown values, so reading a property gives unknown and forces a check, and it rejects arrays and primitives in practice.

record.tsTypeScript
function keysOf(data: Record<string, unknown>): string[] {
  return Object.keys(data);
}

console.log(keysOf({ a: 1, b: "x" }));
console.log(keysOf(42));
Text
record.ts(6,20): error TS2345: Argument of type 'number' is not assignable to parameter of type 'Record<string, unknown>'.

If you mean "something I know nothing about", use unknown, and narrow with typeof x === "object" && x !== null before treating it as an object. If you mean "anything that is not nullish", use NonNullable<T> or T & {} in a generic. If you mean "a value of some specific shape", write the interface.

Use object when the point really is "not a primitive", for example a function that takes a key for a WeakSet. It is the one of the three with a clean meaning.

Narrowing from unknown

A value of type unknown becomes object through the usual guard, with the null check that typeof needs because typeof null is "object" in JavaScript.

narrow.tsTypeScript
function isObject(x: unknown): x is object {
  return typeof x === "object" && x !== null;
}

const input: unknown = { id: 7 };

if (isObject(input)) {
  console.log("an object");
}
Text
an object

The predicate says the value is a non-primitive, and the narrowed type is object. Functions do not pass this check, since typeof gives "function" for them. If you need to include functions, add that branch.

Practical guidance

Reach for object only when you need to exclude primitives, and for unknown or Record<string, unknown> when you are describing data you will inspect. Avoid {} as a standalone annotation, because people read it as "empty object" and it accepts a number. Keep it in the form T & {} where you want to strip nullish values. Never write Object, String, Number or Boolean as types. In a code base that already contains them, a search for those names is a quick way to find places where a type is looser than it looks.

Common mistakes

The first is annotating with {} to mean "empty object" and then passing a number without an error. The second is using Object and getting rejected for a method that happens to share a name with one of its members. The third is using object for a parameter and then reading properties from it, which needs a narrower type. The fourth is forgetting that these differences depend on strictNullChecks, and that with it off, null is accepted by {}, Object and object alike.

When this does not apply

If a type has a name and a shape, such as User or Options, use it, and none of this matters. The three types in this post are for the cases where you cannot or do not want to name a shape. For generic code, a constraint such as T extends object is a clear way to say "no primitives", and works better than any of the other two.

Share

Keep reading

Flat illustration of an editor window full of code, with the TypeScript, npm and Node.js logos.
TypeScript

Type Inference in TypeScript

TypeScript works out most types for you. Learn how inference treats let and const, arrays, return values and callbacks, and when to write a type yourself.

7 min read
Flat illustration of an editor window full of code, with the TypeScript, Node.js, npm, Vite and JavaScript logos.
TypeScript

What Is TypeScript, and Why Use It?

TypeScript adds static types to JavaScript and checks them before your code runs. Learn what it is, what it catches, what it costs and when to use it.

6 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.