TypeScript
version 2.2 introduced a new type object
. It describes any non-primitive type.
The following types are considered primitive in JavaScript
:
boolean
number
string
symbol
null
undefined
All other types are considered non-primitive .
A new type of object
represents them:
// All primitive types type Primitive = | boolean | number | string | symbol | null | undefined; // All non-primitive types type NonPrimitive = object;
Let's look at how object
will help us more accurately describe types.
object
typeWith the release of TypeScript
version 2.2
, the standard library type descriptions have been updated using the new object
type. For example, the Object.create()
and Object.setPrototypeOf()
methods now describe the prototype parameter as object | null
object | null
:
interface ObjectConstructor { /** * Creates an object that has the specified prototype or that has null prototype. * @param o Object to use as a prototype. May be null. */ create(o: object | null): any; /** * Sets the prototype of a specified object o to object proto or null. Returns the object o. * @param o The object to change its prototype. * @param proto The value of the new prototype or null. */ setPrototypeOf(o: any, proto: object | null): any; // ... }
If you pass a primitive type, as a prototype in Object.setPrototypeOf()
or in Object.create()
, then during the execution of the code, a TypeError
exception will be thrown. Now TypeScript
catches these errors at compile time:
const proto = {}; Object.create(proto); // OK Object.create(null); // OK Object.create(undefined); // Error Object.create(1337); // Error Object.create(true); // Error Object.create("oops"); // Error
Another place to use the object
type is the WeakMap
data WeakMap
. The keys of which must be objects and cannot be primitives. This requirement is reflected in taiping:
interface WeakMap<K extends object, V> { delete(key: K): boolean; get(key: K): V | undefined; has(key: K): boolean; set(key: K, value: V): this; }
object
vs Object
vs {}
You may be confused by the fact that TypeScript
defines several types that have similar names but represent different concepts:
We looked at the new object
type above. Now let's discuss what Object
and {}
.
Object
typeTypescript
defines a different type, with almost the same name as the new type object
, and this is the Object
type. While object
(with a small letter) represents all non-primitive types, Object
(with a capital letter) describes the functionality common to all JavaScript
objects. For example, the toString()
and hasOwnProperty()
methods. In the file lib.es6.d.ts
type Object
is defined as follows:
interface Object { // ... /** Returns a string representation of an object. */ toString(): string; /** Returns a date converted to a string using the current locale. */ toLocaleString(): string; /** Returns the primitive value of the specified object. */ valueOf(): Object; /** * Determines whether an object has a property with the specified name. * @param v A property name. */ hasOwnProperty(v: string): boolean; /** * Determines whether an object exists in another object's prototype chain. * @param v Another object whose prototype chain is to be checked. */ isPrototypeOf(v: Object): boolean; /** * Determines whether a specified property is enumerable. * @param v A property name. */ propertyIsEnumerable(v: string): boolean; }
{}
There is another type that is very similar: {}
, an empty object type. It describes an object that does not have its own properties. When trying to access arbitrary properties of such a TypeScript
object, an error occurs during compilation:
// Type {} const obj = {}; // Error: Property 'prop' does not exist on type '{}'. obj.prop = "value";
However, you can use all the properties and methods described in the Object
type, which are implicitly accessible through the prototype chain:
// Type {} const obj = {}; // "[object Object]" obj.toString();
Source: https://habr.com/ru/post/346718/
All Articles