TypeScript’s type system is the core concept of the language. It allows developers to define types for variables, functions, and objects. For example:
// Defining a variable with a specific type
let message: string = "Hello, TypeScript!";
// Defining a function with input and output types
function add(a: number, b: number): number {
return a + b;
}
Interfaces are used to define the shape of an object. They can describe the properties and their types that an object should have.
interface Person {
name: string;
age: number;
}
let person: Person = {
name: "John",
age: 30
};
Classes in TypeScript are similar to classes in other object - oriented languages. They can have properties, methods, and constructors.
class Animal {
constructor(public name: string) {}
speak() {
console.log(`${this.name} makes a sound.`);
}
}
let dog = new Animal("Dog");
dog.speak();
To use TypeScript, you first need to install it globally using npm:
npm install -g typescript
After writing TypeScript code in a .ts
file, you can compile it to JavaScript using the tsc
command. For example, if you have a file named app.ts
, you can compile it with:
tsc app.ts
You can use TypeScript in a Node.js project by initializing a tsconfig.json
file. This file contains the compiler options for your project.
npx tsc --init
Type assertion is used when you know the type of a value better than the TypeScript compiler. It allows you to override the type inference.
let someValue: any = "this is a string";
let strLength: number = (someValue as string).length;
Optional chaining is a feature that allows you to access nested properties safely without worrying about null or undefined values.
interface User {
address?: {
street?: string;
};
}
let user: User = {};
let street = user?.address?.street;
Nullish coalescing is used to provide a default value when a variable is null
or undefined
.
let value: string | null = null;
let result = value?? "default value";
While type annotations are useful, over - annotating can make the code verbose. Let TypeScript infer types whenever possible.
// Good practice: Let TypeScript infer the type
let num = 10;
// Bad practice: Unnecessary type annotation
let numAnnotated: number = 10;
Interfaces should be easy to understand and maintain. Use descriptive names and group related properties together.
interface UserProfile {
firstName: string;
lastName: string;
email: string;
}
Functions and classes should have a single responsibility. This makes the code easier to test and maintain.
// Good practice: Single responsibility function
function calculateSum(numbers: number[]): number {
return numbers.reduce((acc, num) => acc + num, 0);
}
“Effective TypeScript 2nd Edition” PDF is a treasure trove of knowledge for TypeScript developers. By understanding the fundamental concepts, mastering the usage methods, following common practices, and adopting best practices, you can write more robust, maintainable, and efficient TypeScript code. Whether you are a beginner or an experienced developer, this book can significantly enhance your TypeScript skills.