DEV Community

Cover image for Differences Between Object, {}, and object in TypeScript
Dipak Ahirav
Dipak Ahirav

Posted on • Edited on

Differences Between Object, {}, and object in TypeScript

TypeScript provides a robust type system that can sometimes be confusing, especially when dealing with similar types like Object, {}, and object. Let's break down the differences and learn how to use them effectively.

please subscribe to my YouTube channel to support my channel and get more web development tutorials.

What is Object?

Definition:

  • Object refers to non-primitive types, meaning any value that isn't a number, string, boolean, symbol, null, or undefined.

Characteristics:

  • It includes all objects and functions.
  • It is an alias for the object type.
  • Allows for primitive wrapper objects like new Number(1) or new String('a').

Example Usage:

let a: Object = {}; // valid, because {} is an object
let b: Object = []; // valid, because [] is an array, which is an object
let c: Object = 42; // invalid, because 42 is a number, a primitive type
let d: Object = "hello"; // invalid, because "hello" is a string, a primitive type
let e: Object = new Number(1); // valid, because new Number(1) is an object
Enter fullscreen mode Exit fullscreen mode

What is {}?

Definition:

  • {} represents an empty object type, meaning an object with no specific properties defined.

Characteristics:

  • The most permissive type in TypeScript.
  • Any type, including primitives (number, string, boolean, null, undefined), can be assigned to {}.

Example Usage:

let x: {} = {}; // valid
let y: {} = 42; // valid, although 42 is a number, `{}` allows any type
let z: {} = "hello"; // valid, although "hello" is a string, `{}` allows any type
let w: {} = null; // valid
let v: {} = undefined; // valid
Enter fullscreen mode Exit fullscreen mode

What is object?

Definition:

  • object refers to any non-primitive type. It is used when you want to specify that a value must be an object, but you don't care about its specific structure.

Characteristics:

  • Includes objects, arrays, functions, and any other types that aren't primitive.
  • Excludes primitive types like number, string, boolean, symbol, null, and undefined.

Example Usage:

let p: object = {}; // valid
let q: object = []; // valid
let r: object = 42; // invalid, because 42 is a number
let s: object = "hello"; // invalid, because "hello" is a string
let t: object = null; // invalid, because null is a primitive type
let u: object = undefined; // invalid, because undefined is a primitive type
Enter fullscreen mode Exit fullscreen mode

Summary

  • Object: Use this when you want to include all objects and functions, even primitive wrapper objects like new Number(1). Avoid using it for precise type definitions.
  • {}: This is very permissive and can accept any value, including primitives, null, and undefined. Use it when you need maximum flexibility.
  • object: Use this when you want to specify that a value must be a non-primitive type (i.e., an object), but you don't need to define its specific structure. It ensures more type safety than {}.

By understanding these distinctions, you can make better type choices in TypeScript, ensuring your code is both type-safe and easy to understand.

Start Your JavaScript Journey

If you're new to JavaScript or want a refresher, visit my blog on BuyMeACoffee to get started with the basics.

👉 Introduction to JavaScript: Your First Steps in Coding

Follow me for more tutorials and tips on web development. Feel free to leave comments or questions below!

Follow and Subscribe:

Top comments (0)