Part 06 · Advanced TypeScript · 6.1.09

Generics, type transformations, modules và compiler contract

Advanced type tốt bảo tồn quan hệ giữa inputs/outputs; type gymnastics không cải thiện runtime và có thể làm API khó hiểu/chậm compile.

Design rule: dùng generic khi cần liên kết positions/capabilities; chọn overload/discriminated union khi chúng diễn đạt intent rõ hơn conditional type.

1. Generics và constraints

Type parameter hữu ích khi output giữ quan hệ với input. Constraint giới hạn capability mà implementation dùng; inference thường tốt hơn bắt caller chỉ type argument. Generic không validate runtime và không nên dùng để che schema mismatch.

function first<T>(items: readonly T[]): T | undefined {
  return items[0];
}
function sizeOf<T extends {length: number}>(value: T) { return value.length; }

2. Type transformations

keyof/indexed access, mapped/conditional/template-literal types tạo type từ source. Conditional type phân phối trên naked union; infer trích type trong pattern. Utility types thường shallow, không tự deep-copy semantics.

3. Type/value namespaces và module resolution

Type-only import/export giúp tránh runtime edge; declaration files mô tả API JS chưa có source types. Runtime module format/resolution phải khớp compiler, bundler và package exports/types conditions. Path alias chỉ là compiler mapping nếu runtime/bundler không rewrite.

LayerQuestion
Type checkerModule specifier/type declaration resolve thế nào?
Bundler/runtimeImport path, extension, ESM/CJS format có load được không?
Packageexports/types/conditions có cùng public surface không?

4. tsconfig là compiler contract

strict, noUncheckedIndexedAccess, exactOptionalPropertyTypesuseUnknownInCatchVariables biến risk thành feedback. Project references chia graph/build boundary. skipLibCheck giảm time nhưng có thể che declaration conflicts; dùng có lý do và theo dõi.

{
  "compilerOptions": {
    "strict": true,
    "noUncheckedIndexedAccess": true,
    "exactOptionalPropertyTypes": true,
    "useUnknownInCatchVariables": true
  }
}
Interview answer: “I use generics to preserve relationships, not to make every API clever. Compiler flags are a risk contract; runtime validation and module resolution still need independent tests.”
Tài liệu: TypeScript Generics · TSConfig Reference · Modules Reference