Module trong TypeScript: import type, verbatimModuleSyntax và moduleResolution
Trong khóa JavaScript bạn đã dùng ES Modules: import, export, dynamic import(). TypeScript dùng đúng cú pháp đó, nhưng thêm hai vấn đề: import kiểu (thứ biến mất khi biên dịch) và cách tìm file (moduleResolution).
Export và import kiểu
Kiểu export/import như giá trị:
export interface User { id: number; name: string }
export type Role = "admin" | "member";
export function createUser(name: string): User { return { id: Date.now(), name }; }import { createUser, type User } from "./user.ts";
import type { Role } from "./user.ts";
const u: User = createUser("An");import type (hoặc type trước từng tên) nói rõ: đây chỉ là kiểu, xóa đi khi chạy. Sau khi biên dịch, import type { Role } biến mất hoàn toàn.
verbatimModuleSyntax: viết sao chạy vậy
Bật "verbatimModuleSyntax": true trong tsconfig, quy tắc trở nên đơn giản:
- Import không có
type→ giữ nguyên trong JavaScript đầu ra. - Import có
type→ bị xóa. - Import một kiểu mà quên
type→ lỗi.
import { User } from "./user.ts"; // ❌ 'User' is a type and must be imported using a type-only import
import { type User } from "./user.ts"; // ✅Vì sao quan trọng? Node.js (type stripping), esbuild, swc… xử lý từng file riêng lẻ, không biết User là kiểu hay giá trị. Nếu import không đánh dấu, chúng giữ lại và Node báo lỗi "does not provide an export named User". verbatimModuleSyntax bắt lỗi đó ngay trong editor.
TIP
Dự án mới: bật verbatimModuleSyntax và erasableSyntaxOnly. Code của bạn khi đó chạy được trên Node mà không cần build.
module và moduleResolution
module quyết định cú pháp module đầu ra và quy tắc import; moduleResolution quyết định cách tìm file từ chuỗi "./user".
| Tình huống | Cấu hình |
|---|---|
| Code chạy trực tiếp trên Node.js | "module": "nodenext" (tự kéo theo moduleResolution: nodenext) |
| Code đi qua bundler (Vite, webpack, Next.js…) | "module": "esnext", "moduleResolution": "bundler" |
Khác biệt chính:
import { a } from "./utils"; // bundler: ✅ | nodenext: ❌ cần đuôi file
import { a } from "./utils.js"; // nodenext: ✅ (trỏ tới utils.ts khi biên dịch)
import { a } from "./utils.ts"; // cần allowImportingTsExtensions hoặc rewriteRelativeImportExtensions- nodenext làm đúng như Node: ESM bắt buộc có đuôi file, đọc
"type"và"exports"trongpackage.json. - bundler dễ dãi như Vite: không cần đuôi, vẫn hiểu
"exports".
Khi chạy .ts trực tiếp bằng Node, bạn import bằng đuôi .ts. Nếu vẫn muốn tsc xuất ra .js, bật "rewriteRelativeImportExtensions": true để tsc tự đổi ./utils.ts thành ./utils.js trong đầu ra.
WARNING
TypeScript 6.0 đã đánh dấu deprecated moduleResolution: "node" (node10) và "classic", cùng các giá trị module amd, umd, system. TypeScript 7.0 gỡ hẳn. Nếu dự án cũ còn dùng, chuyển sang nodenext hoặc bundler.
Alias đường dẫn với paths
{
"compilerOptions": {
"paths": { "@/*": ["./src/*"] }
}
}import { db } from "@/lib/db";Từ TS 6.0, baseUrl bị deprecated: paths tính tương đối so với thư mục chứa tsconfig.json, không cần baseUrl nữa.
IMPORTANT
paths chỉ dạy TypeScript cách tìm file, không đổi đường dẫn trong JavaScript đầu ra. Bundler (Vite, Next.js) hoặc "imports" trong package.json ("#lib/*": "./src/lib/*") mới thật sự giải quyết lúc chạy. Với Node, ưu tiên "imports" + subpath #, TypeScript hiểu cả hai.
Tóm tắt
- Đánh dấu kiểu bằng
import type, bậtverbatimModuleSyntax. - Node trực tiếp →
nodenext; qua bundler →bundler. - Bỏ
baseUrl,moduleResolution: nodetrước khi lên TS 7.
Comments