Nhảy tới nội dung

Kiểu undefined

undefined trong JavaScript là giá trị primitive biểu thị chưa được định nghĩa. Xuất hiện khi biến chưa được gán giá trị, hàm không có return value, truy cập property không tồn tại trong object, truy cập index không tồn tại trong array, v.v.

js
let name;
console.log(name);
undefined
 
function func() {}
console.log(func());
undefined
 
const obj = {};
console.log(obj.name);
undefined
 
const arr = [];
console.log(arr[1]);
undefined
js
let name;
console.log(name);
undefined
 
function func() {}
console.log(func());
undefined
 
const obj = {};
console.log(obj.name);
undefined
 
const arr = [];
console.log(arr[1]);
undefined

undefined literal

Trong JavaScript, các primitive type như boolean hay number có literal, nhưng undefined không có literal. Thực ra undefined là một biến. Có thể hiểu nó như một global constant.

Type annotation của undefined

Để type annotation cho undefined trong TypeScript, sử dụng undefined.

ts
const x: undefined = undefined;
ts
const x: undefined = undefined;

Hàm không có return value sẽ trả về undefined, nhưng trong TypeScript để type annotation cho hàm không có return value, dùng void thay vì undefined. Xem chi tiết tại phần giải thích về hàm.

📄️ Hàm không có return value và kiểu void

Trong TypeScript, để type annotation cho return value của hàm không có return value, sử dụng kiểu void. Kiểu void là kiểu đặc biệt dùng để type annotation cho return value của hàm.