Type annotation là khai báo kiểu tường minh cho biến, parameter, return value: let name: string = 'John'.
- TypeScript cũng có type inference tự suy luận kiểu từ giá trị gán.
- Annotation thường chỉ cần cho function parameters, return types, và khi inference không đủ thông tin.
typescript
function greet(name: string): string {
return 'Hi ' + name;
}
let count: number = 0; // annotation thường dư (inference đủ)
let data: unknown; // cần annotation vì chưa gánType annotation is an explicit type declaration for variables, parameters, and return values: let name: string = 'John'.
- TypeScript also has type inference to automatically infer types from assigned values.
- Annotations are usually only needed for function parameters, return types, and when inference lacks sufficient information.
typescript
function greet(name: string): string {
return 'Hi ' + name;
}
let count: number = 0; // annotation often redundant (inference sufficient)
let data: unknown; // needed when no initial value