タイプスクリプト 型オブジェクト

TypeScriptバージョン2.2では、新しいタイプのobject導入されました。 非プリミティブ型を記述します。
JavaScriptでは、次のタイプがプリミティブと見なされJavaScript



他のすべてのタイプは非プリミティブと見なされます。


新しい型objectはそれらを正確に表します:


 // All primitive types type Primitive = | boolean | number | string | symbol | null | undefined; // All non-primitive types type NonPrimitive = object; 

objectがどのように型をより正確に記述するのに役立つかを見てみましょう。


objectタイプを使用したタイプの説明


TypeScriptバージョン2.2リリースで、標準ライブラリタイプの説明は新しいobjectタイプを使用して更新されました。 たとえば、 Object.create()およびObject.setPrototypeOf()メソッドは、prototypeパラメーターを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; // ... } 

プリミティブ型をプロトタイプとしてObject.setPrototypeOf()またはObject.create()に渡すと、コードの実行中にTypeError例外がスローされます。 TypeScriptは、コンパイル時にこれらのエラーをキャッチするようになりました。


 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 

objectタイプを使用する別の場所は、 WeakMapデータWeakMapです。 キーはオブジェクトでなければならず、プリミティブにはできません。 この要件はチップに反映されます。


 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 {}


TypeScriptは、類似した名前を持っているが異なる概念を表すいくつかの型が定義されていると混同される場合があります。



上記の新しいタイプのobjectを調べました。 それでは、 Object{}説明しましょう。


タイプObject


Typescriptは、新しいobjectタイプとほぼ同じ名前の異なるタイプを定義しobject 。これはObjectタイプです。 object (小文字)はすべての非プリミティブ型を表しますが、 Object (大文字)はすべてのJavaScriptオブジェクトに共通の機能を表します。 たとえば、 toString()およびhasOwnProperty()メソッド。 ファイルlib.es6.d.ts Object型は次のように定義されています。


 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; } 

タイプ{}


非常に類似した別のタイプがあります: {} 、空のオブジェクトタイプ。 独自のプロパティを持たないオブジェクトを記述します。 そのようなオブジェクトの任意のプロパティにアクセスしようとすると、 TypeScriptはコンパイル時にエラーをスローします。


 // Type {} const obj = {}; // Error: Property 'prop' does not exist on type '{}'. obj.prop = "value"; 

ただし、プロトタイプチェーンを介して暗黙的にアクセスできるObjectタイプで説明されているすべてのプロパティとメソッドを使用できます。


 // Type {} const obj = {}; // "[object Object]" obj.toString(); 



Source: https://habr.com/ru/post/J346718/


All Articles