Nhảy tới nội dung

Function declaration

Function declaration là cú pháp để định nghĩa function trong JavaScript.

Cú pháp function declaration​

Function declaration trong JavaScript sử dụng cú pháp function.

js
function hello() {
return "hello";
}
js
function hello() {
return "hello";
}

Type annotation cho function declaration​

Trong TypeScript, bạn có thể viết type annotation cho tham số và giá trị trả về của function declaration.

ts
function increment(num: number): number {
return num + 1;
}
ts
function increment(num: number): number {
return num + 1;
}

Khi bỏ qua type annotation cho tham số, compiler sẽ ngầm hiểu nó là kiểu any.

ts
function increment(num): number {
(parameter) num: any
return num + 1;
}
ts
function increment(num): number {
(parameter) num: any
return num + 1;
}

Bằng cách đặt compiler option noImplicitAny thành true, bạn có thể bắt buộc type annotation cho tham số.

ts
function increment(num): number {
Parameter 'num' implicitly has an 'any' type.7006Parameter 'num' implicitly has an 'any' type.
return num + 1;
}
ts
function increment(num): number {
Parameter 'num' implicitly has an 'any' type.7006Parameter 'num' implicitly has an 'any' type.
return num + 1;
}

📄️ noImplicitAny

Cấm any type ngầm định

Khi bỏ qua type annotation cho giá trị trả về, compiler sẽ suy luận kiểu từ code.

ts
function increment(num: number) {
return num + 1;
}
const value = increment(1);
function increment(num: number): number
ts
function increment(num: number) {
return num + 1;
}
const value = increment(1);
function increment(num: number): number

Khi có nhiều return và trả về các kiểu khác nhau, kiểu được suy luận sẽ là union type.

ts
function getFirst(items: number[]) {
if (typeof items[0] === "number") {
return items[0];
}
return null;
}
 
getFirst([1, 2, 3]);
function getFirst(items: number[]): number | null
ts
function getFirst(items: number[]) {
if (typeof items[0] === "number") {
return items[0];
}
return null;
}
 
getFirst([1, 2, 3]);
function getFirst(items: number[]): number | null