DEV Community

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

Posted on • Edited on

2

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:

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay