7 Advanced TypeScript Tricks for Clean Code

Welcome to the world of TypeScript! As an aspiring front-end developer, delving into the intricacies of TypeScript can significantly elevate your coding skills and empower you to write code that is not only clean but also maintainable. In this article, we will explore seven advanced TypeScript tricks that will provide valuable insights and take your coding prowess to new heights. Let's dive in!

1. Embrace the Power of Optional Chaining

Nested objects and the lurking null and undefined errors can be quite a hassle. Thankfully, TypeScript's optional chaining feature comes to the rescue! Instead of grappling with verbose null checks, we can elegantly access nested properties using a simple syntax:

const city = user?.address?.city;

By employing optional chaining, we ensure that our code gracefully handles situations where any of the nested properties are null or undefined. Say goodbye to runtime errors and revel in improved code readability.

2. Unleash the Potential of Type Guards

TypeScript's type guards are the superheroes that help us narrow down the type of a variable within conditional statements. Imagine a function that needs to adapt its behavior based on the type of the argument:

function processValue(value: string | number) {
  if (typeof value === "string") {
    // Process the string value
  } else {
    // Process the number value

Here, the typeof type guard allows us to determine whether value is a string or a number, enabling us to perform the appropriate operations. Type guards provide a shield of confidence and ward off potential type-related bugs in your code.

3. Unlock the Potential of Mapped Types

Mapped types are hidden gems within TypeScript, allowing us to create new types based on existing ones. They offer a pathway to reducing code duplication and increasing reusability. Suppose we have an interface called User with several properties. We can craft a read-only version of this interface using a mapped type:

interface User {
  name: string;
  age: number;
  email: string;

type ReadOnlyUser = {
  readonly [K in keyof User]: User[K];

The ReadOnlyUser type generates a read-only variant of the User interface, ensuring that the properties cannot be modified. Mapped types bestow upon us the power of flexibility and customization to cater to various transformation requirements.

4. Harness the Potential of Intersection and Union Types

Intersection and union types are formidable tools in TypeScript's arsenal, allowing us to combine multiple types into one. Intersection types (&) merge properties and methods from different types, empowering us to create comprehensive and expressive types. Union types (|) represent values that can be of different types, offering flexibility in handling diverse scenarios. Let's consider an example:

type Admin = {
  id: number;
  role: "admin";

type User = {
  id: number;
  role: "user";

type AdminUser = Admin & User;

In this instance, the AdminUser type combines properties from both the Admin and User types, enabling us to represent a user with both admin and user roles. Union types, on the other hand, facilitate scenarios where a value can be of multiple types.

5. Embrace the Beauty of Type Inference

TypeScript's type inference mechanism acts as a loyal companion, automatically deducing the types of variables based on their usage. This captivating feature saves us from explicitly annotating types in every scenario, resulting in code that is both clean and concise. Consider the following example:

function multiply(a

: number, b: number) {
  return a * b;

const result = multiply(5, 10);

In this case, TypeScript infers that result is of type number because the multiply function returns a number. Type inference not only reduces clutter but also improves developer productivity.

6. Use Enums for Constants

When working with constants in your code, TypeScript Enums provide a clean and readable approach. Enums allow you to define a set of named values, making your code more self-explanatory and less error-prone. Let's see an example:

enum Direction {
  Up = "UP",
  Down = "DOWN",
  Left = "LEFT",
  Right = "RIGHT",

function move(direction: Direction) {
  // Logic based on direction


In this case, using an Enum ensures that the direction parameter is limited to the defined values, reducing the chances of passing incorrect values and improving code maintainability.

7. Employ Type Narrowing with Discriminated Unions

Discriminated unions, also known as tagged unions or algebraic data types, allow you to combine different types under a single umbrella type. This technique improves type safety and code clarity. Let's illustrate with an example:

type Shape = {
  kind: "circle";
  radius: number;
} | {
  kind: "rectangle";
  width: number;
  height: number;

function calculateArea(shape: Shape) {
  if (shape.kind === "circle") {
    const area = Math.PI * shape.radius * shape.radius;
    // Process circle area
  } else if (shape.kind === "rectangle") {
    const area = shape.width * shape.height;
    // Process rectangle area

By using the kind property as a discriminant, TypeScript narrows down the type of shape within each conditional block. This approach ensures exhaustive checks and enhances type safety in your codebase.

Congratulations! You have ventured into the realm of advanced TypeScript tricks, equipping yourself with powerful techniques to elevate your code to new heights of cleanliness and maintainability. By embracing optional chaining, type guards, mapped types, intersection/union types, type inference, Enums, and discriminated unions, you can craft expressive, type-safe, and efficient code. These tricks serve as valuable assets for any beginner software developer seeking to embark on their TypeScript journey. Embrace these techniques, practice diligently, and immerse yourself in the world of clean coding. Happy coding!