Static typing in TypeScript allows you to specify the types of variables, function parameters, and return values. This helps in preventing type - related errors at compile - time.
// Variable with a specific type
let message: string = "Hello, TypeScript!";
// Function with typed parameters and return value
function addNumbers(a: number, b: number): number {
return a + b;
}
Interfaces are used to define the structure of an object. They can be used to enforce a certain contract on the shape of objects.
interface Person {
name: string;
age: number;
greet(): string;
}
let person: Person = {
name: "John",
age: 30,
greet() {
return `Hello, my name is ${this.name} and I'm ${this.age} years old.`;
}
};
TypeScript supports object - oriented programming concepts like classes. Classes are a blueprint for creating objects.
class Animal {
constructor(public name: string) {}
move(distance: number = 0) {
console.log(`${this.name} moved ${distance}m.`);
}
}
class Dog extends Animal {
bark() {
console.log('Woof! Woof!');
}
}
let dog = new Dog('Buddy');
dog.bark();
dog.move(10);
Enums are used to define a set of named constants.
enum Color {
Red,
Green,
Blue
}
let myColor: Color = Color.Green;
console.log(myColor);
You can install TypeScript globally using npm:
npm install -g typescript
TypeScript code needs to be compiled to JavaScript. You can use the tsc
command to compile a TypeScript file.
tsc myfile.ts
Here is a simple example of TypeScript code with basic syntax:
// Variable declarations
let isDone: boolean = false;
let decimal: number = 6;
let name: string = "Alice";
// Array
let list: number[] = [1, 2, 3];
// Tuple
let x: [string, number];
x = ["hello", 10];
Function overloading allows a function to have multiple call signatures.
function add(a: number, b: number): number;
function add(a: string, b: string): string;
function add(a: any, b: any): any {
return a + b;
}
let result1 = add(1, 2);
let result2 = add("Hello ", "World");
Generics provide a way to create reusable components that can work with multiple types.
function identity<T>(arg: T): T {
return arg;
}
let output1 = identity<string>("myString");
let output2 = identity<number>(100);
Enabling strict mode in TypeScript helps catch more errors early. You can set "strict": true
in your tsconfig.json
file.
{
"compilerOptions": {
"strict": true
}
}
Use interfaces to define the shape of objects and enforce contracts between different parts of your code.
Type aliases can be used to create custom names for types, making your code more readable.
type Point = {
x: number;
y: number;
};
function printPoint(point: Point) {
console.log(`(${point.x}, ${point.y})`);
}
TypeScript is a powerful language that brings static typing and other advanced features to JavaScript. By understanding the fundamental concepts, usage methods, common practices, and best practices, you can build more reliable and maintainable applications. Whether you are working on a small project or a large - scale enterprise application, TypeScript can help you write better code.