Step 1: Install TypeScript
First things first, you need to install TypeScript.
Open your terminal and run:
|
|
This installs TypeScript globally, so you can use it in any project like a wizard summoning spells.
If you want to install it locally (because global installs are so 2019), run:
|
|
This keeps your project dependencies clean and prevents conflicts with other projects.
Step 2: Initialize TypeScript in Your Project
Run the following command in your project directory:
|
|
This will generate a tsconfig.json
file, which is basically TypeScript’s rulebook.
You can tweak it, but for now, the default settings should be fine.
If you want a quick setup, just use:
|
|
This makes sure your TypeScript compiles down to something Node.js understands while keeping your code well-organized.
Step 3: Write Your First TypeScript File
Create a src
folder (if you haven’t already) and inside it, create a file called index.ts
.
Now, let’s write some TypeScript magic:
|
|
Notice how we’re actually specifying the type (string
) for the name
parameter and return type?
No more accidental undefined
nightmares!
Step 4: Compile TypeScript to JavaScript
To convert TypeScript into something Node.js understands, run:
|
|
This compiles all .ts
files in the project and outputs them into the dist
directory (or whatever you specified in tsconfig.json
).
Now, you can run your compiled code with:
|
|
And boom!
You just ran TypeScript in Node.js.
Feels good, right?
Step 5: Run TypeScript Directly With ts-node (Optional)
If you don’t want to keep compiling every time, install ts-node
:
|
|
Then, run your TypeScript files directly:
|
|
No compiling needed!
TypeScript on steroids.
Step 6: Use Type Definitions for Node.js
Node.js doesn’t know what TypeScript is by default, so you’ll need to install type definitions:
|
|
This gives TypeScript the power to understand Node.js APIs, making your autocompletion dreams come true.
Step 7: Enjoy the Benefits
With TypeScript, you now get:
- Type Safety: No more
undefined is not a function
nonsense. - Better Autocompletion: Your editor actually helps instead of mocking you.
- Code Readability: Your future self will thank you.
- More Confidence in Refactoring: Change things without fear!
Key Ideas
Concept | Summary |
---|---|
Install TypeScript | Use npm install -g typescript or npm install --save-dev typescript |
Initialize Project | Run tsconfig.json --init and configure tsconfig.json |
Write TypeScript | Create .ts files and use types for better safety |
Compile to JS | Use tsc to compile TypeScript files |
Run Without Compiling | Use ts-node to run TypeScript directly |
Add Node.js Type Definitions | Install @types/node for better compatibility |