object Type to Exclude Primitives

Pro Members Only

This lesson is available if you have an active subscription.

Alternatively, some member might be able to gift it to you.

If you already have a subscription, you can sign in.

object Type to Exclude Primitives

Subscription Required

You must have an active subscription to access this content.
If you already have a subscription, you can sign in.

Introduction

The object exists explicitly to exclude Primitive types in JavaScript. For example:

type Primitive = boolean | number | string | undefined | null | symbol | bigint;

let example: object;

example = {};
example = [];
example = () => {};

example = { hello: 'hi' };
example = [1, 2, 3];
example = () => 456;

example = 1337;
// ❌ Error: `number` not assignable to `object`

example = 'What does the fox say?';
// ❌ Error: `string` not assignable to `object`

Example Use Case

object is used by the TypeScript library definitions to describe the nature of the useful Object.create method:

const classic = {};

classic['key'] = 'value';
console.log(classic['key']); // value

console.log(classic['toString']); // [Function: toString]

const better = Object.create(null);

better['key'] = 'value';
console.log(better['key']); // value

console.log(better['toString']); // undefined
javascript
typescript
react
playwright

Enjoy free content straight from your inbox 💌

No spam, unsubscribe at any time.

Transcript

00:00

There are certain types in TypeScript that are really useful in everyday TypeScript coding, but they exist in TypeScript to support the JavaScript ecosystem. One such type is the object type. Now the primitive types that are represented within TypeScript are something that we already covered in our TypeScript course, and it's basically Boolean numbers, string, undefined, null and modern ones symbol and big int. And the focus of this lesson is the object type with a small O. Now this particular type accepts anything that is not a primitive type within JavaScript. And this basically means object and object like things.

00:35

So an object literal will be fine and a array will be fine and a functional will be fine. And these objects don't have to be empty. So any object, any array, any function is going to be compatible with this object type. Now anything within JavaScript that is a primitive is not something that we can assign to the object type. So if you try to assign a number, we will get a compile time error. If you try to assign a string, people get a compile time error because these types are not compatible with the object type. If you understand primitive types in JavaScript, it's pretty easy to explain

01:06

what values the object type is incompatible with. But a question you should have at this point is why does this type exist? And the answer is that it exists to support built-in JavaScript APIs. So let's take a look at an example. It's a common pattern within JavaScript to use objects as lightweight key value pairs. For example. By using the index signature, we can set a key to a particular value. And again, using the index signature, we can read the value that we stored as well. However, what you might not realize is that this object has all the properties

01:38

that exist on the object prototype. So even though we didn't explicitly set something like two string, it is something that is going to be present on this object. This is why JavaScript got the method object of create, which allows us to create an object with an explicit prototype. Here we are setting the prototype to null, and now we can still use this created object as a key value pair. So we can set the key to a particular value and then read that value in the future. And it does not get polluted by the standard object prototype because we set it to null. So something like two string is not going to be present on this better object.

02:11

Now the only things that are allowed to be a prototype within JavaScript are non primitive values and null. So if you look at the function signature of object dot create, that is exactly what we see, this object type along with null. So in summary, the object type is not something that you will most likely use in your own APIs, but it is something that the TypeScript team needed in order to model modern JavaScript.