Nhảy tới nội dung

Sự khác biệt giữa object, Object, {}

Trong TypeScript, khi type annotation cho object, thông thường sẽ chỉ định cả type của property.

ts
let obj: { a: number; b: number };
ts
let obj: { a: number; b: number };

📄️ Type annotation của object

Trong TypeScript, type annotation của object được viết giống như JavaScript object literal, với cặp key và type của value cho từng property.

Khác với type annotation thông thường như vậy, cũng có cách type annotation đại khái "là object" mà không chỉ định type của property. Đó là sử dụng object type, Object type, hoặc {} type.

ts
let a: object;
let b: Object;
let c: {};
ts
let a: object;
let b: Object;
let c: {};

Tất cả những type này đều cho phép gán bất kỳ giá trị nào là object type.

ts
const a: object = {}; // OK
const b: Object = {}; // OK
const c: {} = {}; // OK
ts
const a: object = {}; // OK
const b: Object = {}; // OK
const c: {} = {}; // OK

Sự khác biệt giữa object type, Object type, {} type

object type, Object type, {} type có phần tương đồng, nhưng object type có điểm khác biệt với 2 type còn lại.

object type

object type là type chỉ cho phép gán object. Vì giá trị trong JavaScript được chia thành primitive type hoặc object, nên có thể nói object type là type không cho phép gán primitive type.

ts
let a: object;
a = { x: 1 }; // OK
a = [1, 2, 3]; // OK. Array là object
a = /a-z/; // OK. Regular expression là object
 
// Primitive type là NG
a = 1;
Type 'number' is not assignable to type 'object'.2322Type 'number' is not assignable to type 'object'.
a = true;
Type 'boolean' is not assignable to type 'object'.2322Type 'boolean' is not assignable to type 'object'.
a = "string";
Type 'string' is not assignable to type 'object'.2322Type 'string' is not assignable to type 'object'.
ts
let a: object;
a = { x: 1 }; // OK
a = [1, 2, 3]; // OK. Array là object
a = /a-z/; // OK. Regular expression là object
 
// Primitive type là NG
a = 1;
Type 'number' is not assignable to type 'object'.2322Type 'number' is not assignable to type 'object'.
a = true;
Type 'boolean' is not assignable to type 'object'.2322Type 'boolean' is not assignable to type 'object'.
a = "string";
Type 'string' is not assignable to type 'object'.2322Type 'string' is not assignable to type 'object'.

📄️ Tất cả những gì không phải primitive đều là object

Trong JavaScript, tất cả những gì không phải primitive type đều là object. Object không chỉ bao gồm instance tạo từ class, mà còn có bản thân class, array, regular expression.

Object type

Object type là interface. Có thể gán bất kỳ giá trị nào có property như valueOf. Do đó, Object type cũng có thể gán mọi primitive type ngoại trừ nullundefined. Vì các primitive type như string, number có thể có property thông qua auto-boxing.

ts
let a: Object;
a = {}; // OK
 
// Primitive type có thể boxing thì OK
a = 1; // OK
a = true; // OK
a = "string"; // OK
 
// null và undefined là NG
a = null;
Type 'null' is not assignable to type 'Object'.2322Type 'null' is not assignable to type 'Object'.
a = undefined;
Type 'undefined' is not assignable to type 'Object'.2322Type 'undefined' is not assignable to type 'Object'.
ts
let a: Object;
a = {}; // OK
 
// Primitive type có thể boxing thì OK
a = 1; // OK
a = true; // OK
a = "string"; // OK
 
// null và undefined là NG
a = null;
Type 'null' is not assignable to type 'Object'.2322Type 'null' is not assignable to type 'Object'.
a = undefined;
Type 'undefined' is not assignable to type 'Object'.2322Type 'undefined' is not assignable to type 'Object'.

📄️ Boxing

Trong nhiều ngôn ngữ, primitive thường không có field hay method. Để xử lý primitive như object, cần chuyển đổi primitive sang object. Việc chuyển đổi từ primitive sang object được gọi là boxing.

Object type được khuyến nghị không nên sử dụng trong tài liệu chính thức của TypeScript. Lý do là vì có thể gán cả primitive type. Nếu muốn cho phép gán bất kỳ object nào, nên cân nhắc sử dụng object type thay thế.

{} type

{} type là type biểu thị object không có property. Có thể gán bất kỳ giá trị nào có thể có property. Điểm này giống với Object type, có thể gán mọi type ngoại trừ nullundefined.

ts
let a: {};
a = {}; // OK
 
// Primitive type có thể boxing thì OK
a = 1; // OK
a = true; // OK
a = "string"; // OK
 
// null và undefined là NG
a = null;
Type 'null' is not assignable to type '{}'.2322Type 'null' is not assignable to type '{}'.
a = undefined;
Type 'undefined' is not assignable to type '{}'.2322Type 'undefined' is not assignable to type '{}'.
ts
let a: {};
a = {}; // OK
 
// Primitive type có thể boxing thì OK
a = 1; // OK
a = true; // OK
a = "string"; // OK
 
// null và undefined là NG
a = null;
Type 'null' is not assignable to type '{}'.2322Type 'null' is not assignable to type '{}'.
a = undefined;
Type 'undefined' is not assignable to type '{}'.2322Type 'undefined' is not assignable to type '{}'.

Phạm vi gán của object type, Object type, {} type

Tổng hợp phạm vi gán của object type, Object type, {} type như hình sau.

Chia sẻ kiến thức

TypeScript có các type tương tự là object, Object, {}. Tất cả đều là type chỉ "object".

✅Sự khác biệt giữa object, Object, {}
1️⃣object type: Chỉ cho phép gán object
2️⃣Object type, {} type: Cho phép gán object và primitive type ngoại trừ null, undefined

Từ 『Survival TypeScript』

Đăng nội dung này lên X