Getting Started with TypeScript
Welcome to TypeScript! This guide will help you get started with TypeScript development and set up your first project.
What is TypeScript?
TypeScript is a strongly typed programming language that builds on JavaScript. It adds optional static types, classes, and modules to JavaScript, making it easier to write robust, maintainable code for large-scale applications.
Prerequisites
Before starting with TypeScript, you should have:
- Node.js installed (version 12 or later)
- A code editor (we recommend VS Code)
- Basic JavaScript knowledge
Installation
- Install TypeScript globally:
npm install -g typescript
- Check the installation:
tsc --version
Creating Your First TypeScript Project
- Create a new directory and initialize:
mkdir my-ts-project
cd my-ts-project
npm init -y
- Install TypeScript locally:
npm install typescript --save-dev
- Initialize TypeScript configuration:
npx tsc --init
This creates a tsconfig.json file with default settings.
Project Structure
Create a basic project structure:
my-ts-project/
├── src/
│ └── index.ts
├── dist/
├── package.json
└── tsconfig.json
Your First TypeScript File
Create src/index.ts:
// Basic types
const message: string = "Hello, TypeScript!";
const count: number = 42;
const isActive: boolean = true;
// Function with type annotations
function greet(name: string): string {
return `Hello, ${name}!`;
}
// Interface
interface User {
name: string;
age: number;
}
// Class implementation
class UserAccount implements User {
constructor(public name: string, public age: number) {}
describe(): string {
return `${this.name} is ${this.age} years old`;
}
}
// Using the class
const user = new UserAccount("John Doe", 30);
console.log(user.describe());
Compiling TypeScript
- Manual compilation:
npx tsc
- Watch mode for development:
npx tsc --watch
Running Your Code
- Add a build script to package.json:
{
"scripts": {
"build": "tsc",
"start": "node dist/index.js",
"dev": "tsc --watch"
}
}
- Build and run:
npm run build
npm start
Development Tools
VS Code Setup
- Install VS Code
- Install recommended extensions:
- TypeScript TSLint Plugin
- ESLint
- Prettier
Adding Type Checking to Your Editor
VS Code includes built-in TypeScript support, but you can enhance it:
- Enable strict mode in tsconfig.json:
{
"compilerOptions": {
"strict": true
}
}
- Add ESLint for TypeScript:
npm install --save-dev eslint @typescript-eslint/parser @typescript-eslint/eslint-plugin
Create .eslintrc.js:
module.exports = {
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint'],
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended'
]
};
Debugging
- Source Maps
Enable source maps in
tsconfig.json:
{
"compilerOptions": {
"sourceMap": true
}
}
- VS Code Debug Configuration
Create
.vscode/launch.json:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Debug Program",
"program": "${workspaceFolder}/src/index.ts",
"preLaunchTask": "tsc: build - tsconfig.json",
"outFiles": ["${workspaceFolder}/dist/**/*.js"]
}
]
}
Next Steps
- Explore the TypeScript documentation
- Practice with small projects
- Learn about:
- Type annotations
- Interfaces
- Classes
- Generics
- Modules
- Decorators
Common Pitfalls to Avoid
- Don't overuse
any:
// Bad
const data: any = fetchData();
// Good
interface ApiResponse {
id: number;
name: string;
}
const data: ApiResponse = fetchData();
- Don't ignore TypeScript errors:
// Bad
// @ts-ignore
const result = someFunction();
// Good
type Result = ReturnType<typeof someFunction>;
const result: Result = someFunction();
- Don't forget to initialize class properties:
// Bad
class User {
name: string; // Error: Property 'name' has no initializer
// Good
class User {
name: string;
constructor(name: string) {
this.name = name;
}
}
Resources
- Official TypeScript Documentation
- TypeScript Playground
- DefinitelyTyped for type definitions
- Community Discord/Forums
- This tutorial series
Remember:
- Start with strict mode enabled
- Use type annotations wisely
- Leverage your IDE's features
- Write tests early
- Keep your TypeScript knowledge up to date