Khai báo kiểu function (function type declaration)
Trong TypeScript, bạn có thể khai báo kiểu của function. Khai báo kiểu function là việc định nghĩa interface của function mà không chỉ ra implementation.
Cú pháp khai báo kiểu function
Khai báo kiểu function sử dụng type alias. Cú pháp như sau:
tstypetên_kiểu = (tên_tham_số :kiểu_tham_số ) =>kiểu_giá_trị_trả_về ;
tstypetên_kiểu = (tên_tham_số :kiểu_tham_số ) =>kiểu_giá_trị_trả_về ;
Ví dụ, khai báo kiểu cho function nhận kiểu number và trả về kiểu number:
tstypeIncrement = (num : number) => number;
tstypeIncrement = (num : number) => number;
Sử dụng khai báo kiểu làm type annotation
Khai báo kiểu function đã định nghĩa có thể được sử dụng làm type annotation cho arrow function.
tsconstincrement :Increment = (num : number): number =>num + 1;// ^^^^^^^^^Type annotation
tsconstincrement :Increment = (num : number): number =>num + 1;// ^^^^^^^^^Type annotation
Cũng có thể sử dụng cho type annotation của function expression.
tsconstincrement :Increment = function (num : number): number {returnnum + 1;};
tsconstincrement :Increment = function (num : number): number {returnnum + 1;};
Tuy nhiên, không thể sử dụng cho type annotation của function declaration.
Bỏ qua type annotation trong function implementation
Khi sử dụng khai báo kiểu function làm type annotation, type annotation cho tham số và giá trị trả về ở phía implementation có thể được bỏ qua.
tsconstf1 :Increment = (num : number): number =>num + 1;// ↓Cú pháp rút gọnconstf2 :Increment = (num ) =>num + 1;
tsconstf1 :Increment = (num : number): number =>num + 1;// ↓Cú pháp rút gọnconstf2 :Increment = (num ) =>num + 1;
Trong code thực tế, thường viết dạng rút gọn.
Khai báo kiểu function bằng method syntax
Trong TypeScript, ngoài cách khai báo kiểu function bằng arrow function syntax, còn có thể khai báo bằng method syntax.
Method syntaxtstypetên_kiểu = {(tên_tham_số :kiểu_tham_số ):kiểu_giá_trị_trả_về ;};
Method syntaxtstypetên_kiểu = {(tên_tham_số :kiểu_tham_số ):kiểu_giá_trị_trả_về ;};
Arrow function syntax và method syntax chỉ khác cách viết. Hai khai báo kiểu sau là cùng kiểu.
ts// Khai báo kiểu bằng arrow function syntaxtypeIncrement1 = (num : number) => number;// Khai báo kiểu bằng method syntaxtypeIncrement2 = {(num : number): number;};
ts// Khai báo kiểu bằng arrow function syntaxtypeIncrement1 = (num : number) => number;// Khai báo kiểu bằng method syntaxtypeIncrement2 = {(num : number): number;};
Thông thường, khai báo kiểu bằng arrow function syntax. Vì arrow function syntax ngắn gọn và đơn giản hơn.
Khai báo kiểu bằng method syntax đôi khi được sử dụng cho khai báo kiểu của overload function.
Khai báo kiểu function từ function
Trong TypeScript, bạn có thể khai báo kiểu function từ implementation của function. Sử dụng type operator typeof trên giá trị function.
ts// Implementation của functionfunctionincrement (num : number): number {returnnum + 1;}// Khai báo kiểu functiontypeIncrement = typeofincrement ;
ts// Implementation của functionfunctionincrement (num : number): number {returnnum + 1;}// Khai báo kiểu functiontypeIncrement = typeofincrement ;
Chia sẻ kiến thức
・Trong TypeScript có thể khai báo kiểu function
Ví dụ: type Func = (n: number) => number
・Kiểu đã định nghĩa có thể sử dụng trong type annotation
・Trong trường hợp đó, type annotation phía implementation có thể bỏ qua
・Cũng có khai báo kiểu dạng method
・Thông thường sử dụng khai báo kiểu dạng arrow function
・Cũng có thể suy ra kiểu từ function bằng typeof
Từ 『Survival TypeScript』