The differences between object and Object

There are many object types in TypeScript. There is Object, object, class object, and object literal. In this section, we will cover the differences between an Object (uppercase) and an object (lowercase).

The Object type that starts with a capital letter, or the uppercase one, or with the big O represents something ubiquitous, a cross type that is available with every type and object. The capital letter Object carries a common set of functions. Here is the list of its available functions:

toString(): string;
toLocaleString(): string;
valueOf(): Object;
hasOwnProperty(v: string): boolean;
isPrototypeOf(v: Object): boolean;
propertyIsEnumerable(v: string): boolean;

A huge set of types comes under the umbrella of Object. Assigning several different values to an object of type Object shows the flexibility of the type and how broad the potential range of types is:

let bigObject: Object;
bigObject = 1;
bigObject = "1";
bigObject = true;
bigObject = Symbol("123");
bigObject = { a: "test" };
bigObject = () => { };
bigObject = [1, 2, 3];
bigObject = new Date();
bigObject = new MyClass();
bigObject = Object.create({});

The lowercase object coverts everything that is not a number, a string, a boolean, a null, an undefined, or a Symbol. The lowercase object is a subset of the uppercase Object. It contains object literals, dates, functions, arrays, and an instance of an object created with new and create:

let littleObject: object;

littleObject = { a: "test" };
littleObject = new Date();
littleObject = () => { };
littleObject = [1, 2, 3];
littleObject = new MyClass();
littleObject = Object.create({});

In the cases of null and undefined, they are neither object nor Object. They are in a special category and are a subtype of all other types. TypeScript's compiler must be configured with the strict option "strictNullCheck", which is the de-factor configuration value, meaning that even if null and undefined are a subset of all types, only a union of the main type and null or undefined will allow the assignation to either of these two special values:

let acceptNull: number | null = null;
acceptNull = 1;

let acceptUndefined: number | undefined = 1;
acceptUndefined = null;
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.155.187