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).
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ậyBật "verbatimModuleSyntax": true trong tsconfig, quy tắc trở nên đơn giản:
type → giữ nguyên trong JavaScript đầu ra.type → bị xóa.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à moduleResolutionmodule 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"type" và "exports" trong package.json."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.
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.
import type, bật verbatimModuleSyntax.nodenext; qua bundler → bundler.baseUrl, moduleResolution: node trước khi lên TS 7.
Comments