Type assertion overrides TS inference at compile time without any runtime check. value as Type nói với TS "tin tôi, tôi biết kiểu này".
- Dùng khi TS không thể infer (DOM queries, JSON.parse).
- Tránh dùng để cast bừa bãi — sẽ mất type safety.
as unknown as Type(double assertion) là dấu hiệu code có vấn đề.
typescript
const canvas = document.getElementById('canvas') as HTMLCanvasElement;
const data = JSON.parse(json) as UserConfig; // vẫn cần validate runtime