GGistDev

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: commonjs and run with Node without type: "module".

Editor setup

Install the official VS Code TypeScript plugins and enable "typescript.tsserver.experimental.enableProjectDiagnostics": true for better feedback.

Tips

  • Keep strict on from day one
  • Use tsx for scripts and local dev for fastest feedback
  • Add satisfies and as const when you want precise literal inference