Field (field)
Để instance có field trong JavaScript, gán giá trị cho property của object đã instance hóa.
JavaScriptjsclassPerson {}constalice = newPerson ();alice .name = "Alice";
JavaScriptjsclassPerson {}constalice = newPerson ();alice .name = "Alice";
Trong TypeScript, ngoài ra còn cần viết type annotation cho field.
TypeScripttsclassPerson {name : string;}constalice = newPerson ();alice .name = "Alice";
TypeScripttsclassPerson {name : string;}constalice = newPerson ();alice .name = "Alice";
TypeScript sẽ báo lỗi compile khi truy cập field không được viết trong khai báo class.
TypeScripttsclassPerson {}constperson = newPerson ();Property 'age' does not exist on type 'Person'.2339Property 'age' does not exist on type 'Person'.console .log (person .); age
TypeScripttsclassPerson {}constperson = newPerson ();Property 'age' does not exist on type 'Person'.2339Property 'age' does not exist on type 'Person'.console .log (person .); age
Ngay cả khi bỏ qua kiểu khi khai báo field, nếu giá trị được gán trong constructor thì kiểu sẽ được suy luận từ giá trị gán. Trong ví dụ dưới, name có kiểu string vì gán giá trị kiểu string trong constructor.
tsclassPerson {privatename ;constructor(name : string) {this.name =name ;}}
tsclassPerson {privatename ;constructor(name : string) {this.name =name ;}}
Field không khởi tạo và kiểm tra
Khi cả hai compiler option strictNullChecks và strictPropertyInitialization của TypeScript được bật, phần name: string trong ví dụ sau sẽ bị báo lỗi compile. Vì ngay sau khi new Person, name sẽ là undefined.
tsclassPerson {Property 'name' has no initializer and is not definitely assigned in the constructor.2564Property 'name' has no initializer and is not definitely assigned in the constructor.: string; name }constalice = newPerson ();console .log (alice .name );
tsclassPerson {Property 'name' has no initializer and is not definitely assigned in the constructor.2564Property 'name' has no initializer and is not definitely assigned in the constructor.: string; name }constalice = newPerson ();console .log (alice .name );
📄️ strictNullChecks
Làm nghiêm ngặt check null và undefined
📄️ strictPropertyInitialization
Bắt buộc khởi tạo class property
Để vượt qua kiểm tra ngay cả khi hai compiler option này được bật, cần đặt type annotation cho field name là union type như string | undefined.
tsclassPerson {name : string | undefined;}constalice = newPerson ();console .log (alice .name );
tsclassPerson {name : string | undefined;}constalice = newPerson ();console .log (alice .name );
Khởi tạo field bằng constructor
Có thể gán giá trị cho field bằng constructor. Trong constructor, sử dụng this để truy cập field muốn gán giá trị.
TypeScripttsclassPerson {name : string;constructor() {this.name = "Alice";}}
TypeScripttsclassPerson {name : string;constructor() {this.name = "Alice";}}
Cho constructor nhận tham số để có thể chỉ định giá trị field động.
TypeScripttsclassPerson {name : string;constructor(personName : string) {this.name =personName ;}}constalice = newPerson ("Alice");
TypeScripttsclassPerson {name : string;constructor(personName : string) {this.name =personName ;}}constalice = newPerson ("Alice");