The object type represents any non-primitive value. It is used to define values that are not a primitive type (string, number, boolean, symbol, null, or undefined).

Example 1: Function Accepting object or null

declare function create(o: object | null): void;
create({ prop: 0 });  // OK, object is passed
create(null);          // OK, null is allowed
  • The function create accepts an argument that is either an object or null.
  • Passing an object like { prop: 0 } or null works fine.

Example 2: Invalid Types for object

create(42);           // Error: 42 is a number, not an object
create("string");     // Error: "string" is a string, not an object
create(false);        // Error: false is a boolean, not an object
create(undefined);    // Error: undefined is not an object or null
  • object only allows non-primitive values, meaning primitives like number, string, boolean, undefined, and symbol are not assignable to it.

Key Points:

  1. object is a type that represents any value that is not a primitive.
  2. It can be used for objects, arrays, functions, or null, but not for primitive types like numbers or strings.
  3. When you use object, you can’t pass primitives (like number, string, boolean) but you can pass any non-primitive value, including null.