😀Cách để compile file ts sang js chạy luôn trên trình duyệt (ok)
Đọc bài này có giải pháp tốt hơn: https://learnreact.gitbook.io/learnreact/type-script/cau-hinh-nay-rat-quan-trong-lib-no-giup-chay-js-truc-tiep-trong-trinh-duyet-ok
{
"name": "typescript",
"version": "1.0.0",
"main": "app.ts",
"scripts": {
"build": "tsc",
"dev": "npx tsx app.ts"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@types/systemjs": "^6.15.1",
"@types/typescript": "^2.0.0",
"tsx": "^4.19.2",
"typescript": "^5.7.3"
},
"dependencies": {
"@types/react": "^19.0.8",
"react": "^19.0.0"
}
}
Chú ý 1: Không sử dụng CommonJS vì nếu sử dụng CommonJS nó sẽ compile ra require mà trình duyệt không hỗ trợ, trình duyệt chỉ hỗ trợ import

Chú ý 2: Ghi thêm đuôi .js nhưng thực chất nó sẽ gọi file add.ts mục đích sau khi đuôi .js nó sẽ compile ra cũng đuôi .js như import {add} from "./add/add.js" còn nếu không ghi nó chỉ sinh ra như sau import {add} from "./add/add" trên trình duyệt nó sẽ không hiểu là gì.


PreviousTrình duyệt không có require, nhưng Node.js thì có. Với Browserify để trình duyệt cũng có (ok)NextWhat does "tsc -p" do in node CLI (ok)
Last updated