...

/

Common TypeScript Features

Common TypeScript Features

Discover different TypeScript features like spread parameters, template strings, generics, optional chaining, and nullish coalescing.

TypeScript has some general features that don’t apply to classes, functions, or parameters, but make coding more efficient and fun. The idea is that the fewer lines of code we write, the better it is. It’s not only about fewer lines, but also about making things more straightforward. There are many such features in ES6 that TypeScript has also implemented. In the following sections, we’ll name a few that will likely be used in an Angular project.

Spread parameter

A spread parameter uses the same ellipsis syntax as the rest parameter but is used inside the body of a function. Let’s illustrate this with an example:

Press + to interact
const newItem = 3;
const oldArray = [1, 2];
const newArray = [...oldArray, newItem];

In the code above, we add an item to an existing array without changing the old one. The old array still contains 1 and 2, whereas the new array contains 1, 2, and 3. The current behavior is called immutability, which means not changing the old array but rather creating a new state from it. It is a principle used in functional programming as a paradigm and for performance reasons.

We can also use a ...