Node.js รัน TypeScript โดยตรง v23.6+: node app.ts ไม่ต้อง tsc/esbuild
返回文章列表

Node.js รัน TypeScript โดยตรง v23.6+: node app.ts ไม่ต้อง tsc/esbuild

Node.js 23.6+ รัน .ts/.mts/.cts native type stripping ESM/CommonJS production setup VS Code config migration guide performance vs tsx ts-node

Software 更新: January 9, 2026

Node.js รัน TypeScript โดยตรงตั้งแต่ v23.6.0 - node app.ts ไม่ต้องคอมไพล์!

Node.js 23.6+ รองรับไฟล์ .ts/.mts/.cts แบบ native ด้วย type stripping ลบ type annotation รันทันที รองรับ ESM/CommonJS

Node.js TypeScript Support

ไฟล์รองรับหมายเหตุ
.tsStandard TypeScript
.mtsES Module TS
.ctsCommonJS TS
.tsxReact JSX

รัน TypeScript ทันที

# ง่ายสุด
node app.ts

# ESM project
node --loader tsx app.ts

# Experimental features
node --experimental-transform-types app.ts

ตัวอย่างใช้งานจริง

app.ts

// Type annotations ลบอัตโนมัติ
interface User {
id: number;
name: string;
}

async function getUser(id: number): Promise<User> {
return { id, name: `User ${id}` };
}

// รันได้เลย node app.ts
const user = await getUser(1);
console.log(user);

package.json

{
"type": "module",
"scripts": {
"dev": "node --watch app.ts",
"start": "node app.ts"
}
}

ESM Import ต้องระบุ .ts

// ✅ ถูกต้อง
import { handler } from './routes.ts';
import express from 'express';

// ❌ Error - ไม่รู้ extension
import { handler } from './routes';

ข้อจำกัดสำคัญ

❌ ไม่มี type checking (ต้อง tsc --noEmit)
❌ tsconfig.json ถูก ignore
❌ Decorators/JSX ต้อง flag
❌ ไม่แปลง const enum/private fields
✅ Syntax TS 99% รองรับ

Production Setup แนะนำ

// tsconfig.json (type check เท่านั้น)
{
"tsBuildInfoFile": ".cache/tsbuildinfo",
"noEmit": true,
"strict": true
}
// package.json
{
"scripts": {
"typecheck": "tsc --noEmit",
"dev": "tsx watch src/index.ts",
"start": "node dist/index.js",
"build": "tsc"
}
}

Performance Comparison

MethodCold StartMemoryBuild Step
node app.ts25ms80MB❌ None
tsc → node35ms75MB✅ Type check
tsx45ms120MB⚠️ Runtime

VS Code Config

// settings.json
{
"typescript.preferences.includePackageJsonAutoImports": "on",
"typescript.suggest.autoImports": true,
"node.experimentalTypeAcquisition": true
}

Migration Guide

1. Node.js >= 23.6.0 ✅
2. package.json "type": "module"
3. ระบุ .ts extension ทุก import
4. tsc --noEmit (type check)
5. node app.ts 🚀
D

DRITESTUDIO

DRITESTUDIO COMPANY LIMITED - Cloud, VPS, Hosting and Colocation provider in Thailand

管理您的 Cookie 设置

我们使用不同类型的 Cookie 来优化您在网站上的体验。点击下方类别了解更多信息并自定义您的偏好设置。请注意,阻止某些类型的 Cookie 可能会影响您的体验。

必要 Cookie

这些 Cookie 对于网站正常运行至关重要。它们支持页面导航和访问安全区域等基本功能。

查看使用的 Cookie
  • 会话 Cookie(会话管理)
  • 安全 Cookie(CSRF 保护)
始终开启

功能性 Cookie

这些 Cookie 启用语言偏好和主题设置等个性化功能。没有这些 Cookie,某些功能可能无法正常工作。

查看使用的 Cookie
  • lang(语言偏好)
  • theme(深色/浅色模式)

分析性 Cookie

这些 Cookie 通过匿名收集和报告信息,帮助我们了解访问者如何与网站互动。

查看使用的 Cookie
  • _ga(Google Analytics)
  • _gid(Google Analytics)

营销 Cookie

这些 Cookie 用于跨网站追踪访问者,以便根据您的兴趣展示相关广告。

查看使用的 Cookie
  • 广告 Cookie
  • 再营销像素

隐私政策