Hello, World in TypeScript
Your first TypeScript program and common ways to run it.
Minimal program
// hello.ts
function greet(name: string): string {
return `Hello, ${name}!`;
}
console.log(greet("TypeScript"));
Ways to run TypeScript
- tsx (recommended for DX)
- ts-node (classic REPL/runner)
- tsc + node (compiled output)
# 1) tsx (no config required for basics)
npx tsx hello.ts
# 2) ts-node
npx ts-node hello.ts
# 3) tsc + node
npx tsc hello.ts --target es2020 --module commonjs
node hello.js
tsconfig for projects
Create a tsconfig.json to control language features and module output.
{
"compilerOptions": {
"target": "ES2020",
"module": "ESNext",
"moduleResolution": "Bundler",
"strict": true,
"skipLibCheck": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"outDir": "dist",
"sourceMap": true
},
"include": ["src"]
}
ESM vs CJS
- Node supports ESM natively. Prefer ESM (
module: ESNext) with modern toolchains. - For CJS targets, set
module: commonjsand run with Node withouttype: "module".
Editor setup
Install the official VS Code TypeScript plugins and enable "typescript.tsserver.experimental.enableProjectDiagnostics": true for better feedback.
Tips
- Keep
stricton from day one - Use
tsxfor scripts and local dev for fastest feedback - Add
satisfiesandas constwhen you want precise literal inference