As we’ve studied TypeScript, we’ve noticed that TypeScript defines types with similar names but different concepts. Let’s discuss the difference between object and {}.

object

The object type represents a non-primitive object. Number, string, Boolean, symbol, null, or undefined are primitive types in JavaScript

With the object type, apis like object. create can be better represented. In the TypeScript2.2 release, the standard library type declarations have been updated to use the new object types. Object, for example, the create () and Object setPrototypeOf () method, now need to account for their prototype parameter specifies the Object | null type:

declare function create(o: object | null): void;

create({ prop: 0 }); // OK
create(null); // OK
create(42); // Error
create("string"); // Error
create(false); // Error
create(undefined); // Error
Copy the code

Object

Another TypeScript type with the same name as object is object. This type is the type of all instances of the Object class

In short, an Object is an Object, but it contains all the common functions of js. See the TS source code for details on Object

In TypeScript, we use interfaces to define object types. What is an interface??

In object-oriented programming, an interface is a specification that defines behavior and actions. In programming, an interface serves as a limitation and specification. An interface defines the specification that a class needs to follow. The interface does not care about the internal state data of the class, nor does it care about the implementation details of the methods in the class. Interfaces in typescrip are similar to Java, but with the addition of more flexible interface types, including properties, functions, indexable, and classes

  • ObjectThe interface defines properties on the object. prototype Object
Interface Person {name: string; age: number; } let tom: Person = { name: 'Tom', age: 25 }; // success let tom: Person = { name: 11, age: 25 }; // ErrorCopy the code

All instances of class Object inherit all properties from the Object interface

Remember! An Object is not an Object. Don’t confuse them

let a: object
let b: Object

a = '1' // Error Type '"1"' is not assignable to type 'object'

a = {} // Success

b = '1' // Success

b = {} // Success
Copy the code

Empty type {}

Empty object: This describes an object that has no members. TypeScript produces a compile-time error when you try to access any property of such an object. However, you can still use all the properties and methods defined on the Object type. These properties and methods can be used implicitly through JavaScript’s prototype chain, for example:

let a = {}

a.prop = '123' // Error Property 'prop' does not exist on type '{}'

a.toString() // "[object Object]"
Copy the code

In JavaSCript, it’s easy to attribute objects, but in TypeScript, doing so causes an error

// JavaScript
const a = {}; 
a.x = 3; 
a.y = 4;

// TypeScript
const a = {}; 
a.x = 3; // Error Property 'x' does not exist on type '{}'
a.y = 4; // Error Property 'y' does not exist on type '{}'
Copy the code

This is because the type of a above is inferred from {}, and it can assign values to existing attributes. If we need to create objects step by step, we can eliminate TYpeScript’s type checking by using type assertion AS

const a = {} as Point;
a.x = 3
a.y = 4 // Success
Copy the code

A better approach is to declare the type of the variable and build the object once

const a: Point = {
    x: 3,
    y: 4
}
Copy the code