Getting Started with TypeScript Basics
Learn the fundamentals of TypeScript and how to set up your first project.
Introduction to TypeScript
TypeScript is a superset of JavaScript that adds optional static typing and other features to improve the development experience.## Step 1: Install Node.js and TypeScript To get started with TypeScript, you need to have Node.js installed on your computer. You can download it from the official Node.js website. Once you have Node.js installed, you can install TypeScript using npm by running the following command in your terminal:
npm install -g typescript
Step 2: Create a New TypeScript Project
Create a new folder for your project and navigate to it in your terminal. Then, run the following command to create a new TypeScript project:
tsc --init
This will create a tsconfig.json file in your project folder, which is used to configure the TypeScript compiler.
Step 3: Write Your First TypeScript Code
Create a new file called greeter.ts and add the following code:
function greet(name: string) {
console.log(`Hello, ${name}!`);
}
greet('Alice');
This code defines a greet function that takes a name parameter and logs a greeting message to the console.
Step 4: Compile and Run Your Code
To compile your TypeScript code, run the following command in your terminal:
tsc greeter.ts
This will generate a greeter.js file in your project folder, which you can run using Node.js:
node greeter.js
You should see the greeting message printed to the console.