TypeScript
バージョン2.2では、新しいタイプのobject
導入されました。 非プリミティブ型を記述します。
JavaScript
では、次のタイプがプリミティブと見なされJavaScript
。
-
boolean
-
number
-
string
-
symbol
-
null
-
undefined
他のすべてのタイプは非プリミティブと見なされます。
新しい型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();