Type annotations allow you to specify the type of a variable, function parameter, or return value. Here is a simple example:
let message: string = "Hello, TypeScript!";
function greet(name: string): string {
return `Hello, ${name}!`;
}
In the above code, message
is explicitly typed as a string
, and the greet
function takes a string
parameter and returns a string
.
TypeScript can automatically infer the type of a variable based on its initial value. For example:
let number = 10; // TypeScript infers the type as number
You don’t always need to explicitly provide type annotations when TypeScript can infer the type correctly.
Interfaces are used to define the structure of an object. They can be used to enforce a certain shape for objects passed as parameters or returned from functions.
interface Person {
name: string;
age: number;
}
function printPerson(person: Person) {
console.log(`${person.name} is ${person.age} years old.`);
}
const person: Person = { name: "John", age: 30 };
printPerson(person);
Enums allow you to define a set of named constants. They are useful when you have a fixed set of values that a variable can take.
enum Color {
Red,
Green,
Blue
}
let favoriteColor: Color = Color.Green;
console.log(favoriteColor); // Output: 1
By default, enums start with a value of 0 for the first member and increment by 1 for each subsequent member.
To install TypeScript globally, you can use npm (Node Package Manager):
npm install -g typescript
To install it as a development dependency in a project, use:
npm install --save-dev typescript
TypeScript code needs to be compiled to JavaScript before it can be run in a browser or Node.js environment. You can compile a TypeScript file using the tsc
command:
tsc app.ts
This will compile the app.ts
file to a JavaScript file with the same name (app.js
).
You can gradually introduce TypeScript into an existing JavaScript project. First, create a tsconfig.json
file in the root of your project:
npx tsc --init
This will generate a basic tsconfig.json
file with default settings. Then, start renaming your .js
files to .ts
and gradually add type annotations.
Function overloading allows you to define multiple function signatures for the same function. It is useful when a function can accept different types and numbers of parameters.
function add(a: number, b: number): number;
function add(a: string, b: string): string;
function add(a: any, b: any): any {
return a + b;
}
const numResult = add(1, 2); // TypeScript knows the result is a number
const strResult = add("Hello", " World"); // TypeScript knows the result is a string
Generics allow you to create reusable components that can work with different types. They provide a way to parameterize types.
function identity<T>(arg: T): T {
return arg;
}
const result = identity<string>("Hello"); // Explicitly specifying the type argument
const numResult = identity(10); // TypeScript infers the type argument as number
Optional chaining (?.
) allows you to safely access nested properties of an object without having to check for null or undefined at each level. Nullish coalescing (??
) provides a default value when a variable is null
or undefined
.
const user = {
address: {
street: "123 Main St"
}
};
const street = user?.address?.street; // Safely access the street property
const value = null;
const defaultValue = value ?? "Default"; // defaultValue will be "Default"
import
and export
statements to share code between modules.ts-jest
to compile TypeScript code during testing.TypeScript 5 offers a wide range of features and improvements that make it a powerful tool for JavaScript development. By understanding the fundamental concepts, usage methods, common practices, and best practices outlined in this blog post, you can write more robust, maintainable, and scalable code. Whether you are working on a small project or a large enterprise application, TypeScript 5 can help you catch errors early and improve the overall quality of your code.