TypeScript Reference ยท Primitive and Special Types
The object type in TypeScript
Learn the lowercase object type in TypeScript: what values it accepts, what it rejects, and why it is rarely the right choice compared with real shapes.
The lowercase object type means "any value that is not a primitive". Objects, arrays, functions, class instances, Maps and Dates all qualify. Strings, numbers, booleans, bigint, symbol, null and undefined do not.
It is easy to confuse with Object (capital O) and {}, which behave differently; see Object and {}. The key point of lowercase object is that it says almost nothing about the value. You cannot read any property from it, so it is mostly useful as a constraint or a parameter type for functions that treat their argument as an opaque reference.
let thing: object = { id: 1 }; thing = [1, 2, 3]; thing = () => 42;
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; function f(o: object): void {} function g<T extends object>(o: T): T { return o; }
| Part | Description |
|---|---|
object |
Any value of a non-primitive type. |
| Allowed | Plain objects, arrays, functions, classes, instances, Map, Set, Date, RegExp and so on. |
| Rejected | string, number, boolean, bigint, symbol, null, undefined. |
Edge cases:
nullis rejected whenstrictNullChecksis on (part ofstrict).- An object literal assigned to a variable of type
objectloses its property types for reads; use a real type orsatisfiesif you need them. - Because
objecthas no known members, only members every object has (throughObject.prototype, such astoString) are accessible.
Usage notes
- Prefer specific types. If you know the shape, use an interface or object type. If you do not know the shape,
unknownplus narrowing is usually safer thanobject. - Good uses.
WeakMap<object, V>andWeakSet<object>keys,Object.keys-style helpers, andT extends objectto exclude primitives from a generic. - Not the same as
Record<string, unknown>. Anobjectcan be an array or function.Record<string, unknown>describes things you can index by string. - Arrays and functions count. If you want to exclude them, narrow with
Array.isArrayandtypeof v === "function". - Narrowing from
unknown.typeof v === "object" && v !== nullnarrows toobject, becausetypeof nullis"object"in JavaScript. - Do not confuse with the
Objectconstructor. The valueObjecthas methods likeObject.keys; the typeObjectis a different thing.