Skip to content
Grok

Type Checking (การตรวจสอบประเภท)

การตรวจสอบประเภทพื้นฐาน

ts
function 
greet
(
name
: string) {
console
.
log
(
name
.
toUpperCase
()); // TypeScript ตรวจสอบว่า name เป็น string
}
greet
("John"); // ทำงานได้
greet
(42); // Error: Argument of type 'number' is not assignable to parameter of type 'string'
Argument of type 'number' is not assignable to parameter of type 'string'.

Strict Mode

ts
// ใน tsconfig.json
{
  "compilerOptions": {
    "strict": true // เปิดโหมดตรวจสอบเข้มงวด
  }
}

Type Assertions

ts
const input = document.getElementById("input") as HTMLInputElement;
// หรือ
const input = <HTMLInputElement> document.getElementById("input");