Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The object type in TypeScript represents any non-primitive value. It acts as a strict type constraint enforcing that a value is not a JavaScript primitive (string, number, boolean, symbol, null, undefined, or bigint). Because arrays, functions, and class instances are technically objects in JavaScript, they all satisfy the object type constraint.
let nonPrimitive: object;

// Valid assignments (Non-primitives)
nonPrimitive = {};
nonPrimitive = { key: "value" };
nonPrimitive = [1, 2, 3];
nonPrimitive = () => "function";
nonPrimitive = new Date();

// Invalid assignments (Primitives)
nonPrimitive = 42;        // Error: Type 'number' is not assignable to type 'object'
nonPrimitive = "string";  // Error: Type 'string' is not assignable to type 'object'
nonPrimitive = true;      // Error: Type 'boolean' is not assignable to type 'object'
nonPrimitive = null;      // Error: Type 'null' is not assignable to type 'object'

Property Access Limitations

The object type is structurally opaque regarding custom properties. While it guarantees the value is a non-primitive, it provides no information about the object’s specific shape. Attempting to access custom properties on a variable typed strictly as object will result in a compiler error. However, variables of type object are not completely inaccessible. TypeScript permits access to properties and methods inherent to all JavaScript objects via Object.prototype (such as .toString(), .valueOf(), and .hasOwnProperty()).
let user: object = { name: "Alice", age: 30 };

// Valid: Methods exist on Object.prototype
console.log(user.toString());
console.log(user.hasOwnProperty("name"));

// Error: Property 'name' does not exist on type 'object'.
console.log(user.name); 

Type Distinctions: object vs Object vs {}

A critical technical distinction in TypeScript is the difference between the lowercase object, the uppercase Object, and the empty object literal {}.
  • object (Lowercase): Strictly represents non-primitives. It rejects all primitive values.
  • Object (Uppercase): Describes the functionality common to all JavaScript objects (methods like toString() and hasOwnProperty()). Because JavaScript auto-boxes primitives (wrapping them in their object equivalents like String or Number), Object accepts primitives.
  • {} (Empty Object Type): Represents an object with no known properties. Structurally, it behaves almost identically to Object and also accepts primitive values.
let strictObject: object;
let globalObject: Object;
let emptyObject: {};

// Assigning a primitive
strictObject = "test"; // ERROR: Type 'string' is not assignable to type 'object'.
globalObject = "test"; // VALID: 'string' has Object.prototype methods.
emptyObject = "test";  // VALID: 'string' satisfies the empty shape.

// Assigning null/undefined (assuming strictNullChecks is true)
strictObject = null;   // ERROR
globalObject = null;   // ERROR
emptyObject = null;    // ERROR
Master TypeScript with Deep Grasping Methodology!Learn More