π¦ TypeScript in Node.js
Use TypeScript with Node.js to write typed, modern JavaScript server-side code that runs perfectly on Nodeβs runtime.
βοΈ Setup
- Install Node.js (v14+ recommended): nodejs.org
- Initialize project:
npm init -y
- Install TypeScript & Node types:
npm install typescript @types/node --save-dev
- Create
tsconfig.json
with npx tsc --init
π Example: Hello World Server
Output will appear here...
π‘ Tip: Use npx tsc
to compile your TypeScript files to JavaScript, then run node dist/index.js
(or wherever your compiled files are).
π§ How to Run This Locally
- Write your TypeScript in
src/index.ts
- Compile with
npx tsc
- Run compiled JS with
node dist/index.js
- Use
ts-node
for dev convenience (npm install -D ts-node
) to run TS directly
π Why Use TypeScript with Node.js?
- Static typing catches bugs before runtime
- Better editor autocompletion & intellisense
- Modern JS features with ES modules, async/await, etc.
- Large ecosystem with type definitions via DefinitelyTyped