One of the core features of TypeScript is static typing. In JavaScript, variables can hold values of any type at runtime. TypeScript allows you to define the type of a variable explicitly, which helps in preventing type - related errors.
// Define a variable with a specific type
let age: number = 25;
// This will cause a compilation error because we are trying to assign a string to a number variable
// age = "twenty - five";
Interfaces in TypeScript are used to define the shape of an object. They can be used to enforce a certain structure on objects.
interface Person {
name: string;
age: number;
isStudent: boolean;
}
let person: Person = {
name: "John",
age: 20,
isStudent: true
};
TypeScript supports object - oriented programming concepts like classes. Classes can be used to create objects with properties and methods.
class Animal {
name: string;
constructor(name: string) {
this.name = name;
}
speak() {
console.log(`${this.name} makes a sound.`);
}
}
let dog = new Animal("Dog");
dog.speak();
To start using TypeScript, you first need to install it globally using npm.
npm install -g typescript
TypeScript code needs to be compiled into JavaScript code before it can be run in a browser or a Node.js environment. You can compile a TypeScript file using the tsc
command.
tsc app.ts
To use TypeScript in a Node.js project, you can follow these steps:
npm init -y
npm install --save -dev typescript
tsconfig.json
file:npx tsc --init
.ts
file and compile it using tsc
.When defining functions, it is a good practice to add type annotations to the parameters. This makes the function more self - documenting and helps catch errors early.
function add(a: number, b: number): number {
return a + b;
}
let result = add(3, 5);
readonly
ModifierThe readonly
modifier can be used to make a property of an object or a class immutable.
interface Point {
readonly x: number;
readonly y: number;
}
let point: Point = { x: 10, y: 20 };
// This will cause a compilation error because x is readonly
// point.x = 30;
Union types allow a variable to have one of several types.
let value: string | number;
value = "hello";
value = 10;
Interfaces and types should be as simple as possible. Avoid creating overly complex types that are difficult to understand and maintain.
Enums can be useful in some cases, but they should be used sparingly. Overusing enums can make the codebase more complex.
Adopt a consistent coding style throughout your TypeScript project. This makes the code more readable and easier to collaborate on. You can use tools like ESLint and Prettier to enforce a coding style.
The “Essential TypeScript 5 Third Edition” PDF is a great resource for both beginners and experienced developers. By understanding the fundamental concepts, usage methods, common practices, and best practices of TypeScript, you can write more reliable and maintainable code. TypeScript’s static typing and other features help in catching errors early, which ultimately leads to a better development experience and a more stable application.