Discover the magic of Typescript
Soumaya Erradi

Soumaya Erradi @soumayaerradi

About: Senior Software Developer | International Speaker | Angular Specialist | Exploring Web3 | Electronics Enthusiast

Location:
Italy
Joined:
Aug 4, 2024

Discover the magic of Typescript

Publish Date: Apr 6
12 5

Typescript is the magic wand for JavaScript development when it comes to developer confidence, scalability, and expressiveness. Building robust web applications today requires better tooling and runtime safety.

This article will introduce you to the powerful features of Typescript, from basic concepts to advanced type manipulation techniques that can enhance your code quality and expressiveness.


Why Typescript?

Let’s be honest, Javascript is powerful but it doesn't scale well by default. As applications grow, dynamic typing can become a limitation. Typescript gives us:

  • Predictability: Catching bugs at compile time before they reach users.
  • Tooling superpowers: From autocomplete to inline documentation.
  • Team confidence: A shared language to describe shape and intent.

Typescript is not here to replace Javascript. It compiles to it, so you can adopt it incrementally.


Typescript vs Javascript

Feature Javascript Typescript
Typing Dynamic Static (with inference)
Tooling Limited Rich (IntelliSense, refactoring)
Maintainability Can become messy Easier to scale
Learning Curve Easy Steeper, but rewarding

Javascript gives you flexibility. Typescript gives you confidence.


Getting started with Typescript

You can start with a simple install:

npm install -g typescript
Enter fullscreen mode Exit fullscreen mode

Create a config file:

tsc --init
Enter fullscreen mode Exit fullscreen mode

Enable strict mode. It’s worth it.


Typescript fundamentals

Type annotations & inference

let name: string = "Soumaya";
let age = 33; // inferred as number
Enter fullscreen mode Exit fullscreen mode

Basic types

  • string, number, boolean: Primitive data types.
  • Arrays: string[] or Array<string> — a list of items of a single type.
  • Tuples: [string, number] — a fixed-size array with defined types per index.
  • Enums: For named constants with auto-incrementing values or custom assignments.
enum Direction {
  Up,
  Down,
  Left,
  Right
}
Enter fullscreen mode Exit fullscreen mode

Special types you should know

  • any: Turns off type checking. Avoid unless necessary.
  • unknown: Like any but requires type checking before usage.
  • never: A type that never occurs. Used for functions that throw or never return.

Union & intersection types

Union

let input: string | number;
Enter fullscreen mode Exit fullscreen mode

Represents a variable that can be one of several types.

Intersection

type Admin = { role: string };
type User = { name: string };
type AdminUser = Admin & User;
Enter fullscreen mode Exit fullscreen mode

Combines multiple types into one.


Type guards

function printId(id: string | number) {
  if (typeof id === 'string') {
    console.log(id.toUpperCase());
  } else {
    console.log(id.toFixed(2));
  }
}
Enter fullscreen mode Exit fullscreen mode

Type guards are your runtime safety net. They help Typescript narrow types dynamically.


Interfaces vs type aliases

Use interface when extending shapes. Interfaces are great for object structures.

Use type when composing types, unions, or template literals.

interface Animal {
  name: string;
}
type Dog = Animal & { bark: () => void };
Enter fullscreen mode Exit fullscreen mode

Generics

Generics provide a way to write flexible, reusable functions and types that still enforce strong typing.

function identity<T>(value: T): T {
  return value;
}
Enter fullscreen mode Exit fullscreen mode
  • T is a type variable. It makes the function reusable for any type.
  • Used widely in utility functions, libraries, and components.

Built-in utility types

  • Partial<T> – All properties of T become optional. Useful for update operations.
  • Readonly<T> – Prevents modification of T's properties.
  • Pick<T, K> – Select a subset of T’s properties.
  • Omit<T, K> – Exclude some properties from T.
  • Record<K, T> – Creates a type with keys K and values of type T.

Example

type User = { id: number; name: string; email: string };
type Preview = Pick<User, "id" | "name">;
Enter fullscreen mode Exit fullscreen mode

Preview is a lighter version of User containing only id and name.


Branded types

Branded types add a unique signature to otherwise compatible types, helping prevent accidental mix-ups when different types share the same structure.

type UserId = string & { readonly brand: unique symbol };
function createUserId(id: string): UserId {
  return id as UserId;
}
Enter fullscreen mode Exit fullscreen mode

Adds a "brand" to make structural types nominal. Prevents accidental mixing of similar-looking types.


Discriminated unions

Discriminated unions allow safe type narrowing based on a common property, typically kind.

type Shape = 
  | { kind: 'circle'; radius: number }
  | { kind: 'square'; side: number };

function area(shape: Shape) {
  switch (shape.kind) {
    case 'circle': return Math.PI * shape.radius ** 2;
    case 'square': return shape.side ** 2;
  }
}
Enter fullscreen mode Exit fullscreen mode

Each shape type is distinguished by a kind field.


Template literal types

Template literal types allow you to construct string-like types using patterns.

type Route = `/${string}`;
Enter fullscreen mode Exit fullscreen mode

Creates types from string patterns. Useful for route matching or string constraints.


Conditional types & infer

Conditional types use the ternary syntax to express type logic. Combined with infer, you can extract information from other types.

type Return<T> = T extends (...args: any[]) => infer R ? R : never;
Enter fullscreen mode Exit fullscreen mode

Extracts the return type R from a function T.


Variadic tuple types

Variadic tuple types enable manipulation of tuples by adding or spreading elements dynamically. This is powerful for function arguments and reusable tuple templates.

type AppendNumber<T extends unknown[]> = [...T, number];
Enter fullscreen mode Exit fullscreen mode

Allows dynamic extension of tuples. Great for typing argument lists.


Recursive types

Recursive types reference themselves to represent nested structures. They're key for typing data like trees or JSON-like data.

type Tree<T> = {
  value: T;
  children?: Tree<T>[];
};
Enter fullscreen mode Exit fullscreen mode

Allows types to reference themselves. Useful for representing nested data structures.


Conclusion

Typescript is more than just type safety. It’s about improving the developer experience, writing expressive code, and building scalable applications. Once you go beyond the basics, you uncover an elegant type system capable of modeling complex behaviors and constraints.

TypeScript offers a comprehensive toolkit to create code that is cleaner, safer, and smarter, from encoding logic with conditional types to inferring return values.

Mastering Typescript opens up new levels of control and clarity in your code, whether you're defining APIs, transforming data structures, or building powerful, reusable components.

The magic is real, and it’s all in the types.

Comments 5 total

  • Ezéchiel Amen AGBLA
    Ezéchiel Amen AGBLAApr 6, 2025

    Thank you for your article @soumayaerradi !
    It's great !

    I've one question for you.

    About Interfaces vs types aliases section, what's the difference if I add bark function in my interface ?

    It will be the same as the guy, right?

    Personally, I think it would be nice if you talked about the limitations of interface versus types and vice versa.

    This is just a suggestion 😄

    • Soumaya Erradi
      Soumaya ErradiApr 12, 2025

      Thank you! 😊
      Yes, adding bark to the interface would make it structurally the same.
      The real differences show up in more advanced cases (for example interface supports merging, while type handles unions and complex compositions better)
      Great suggestion, I’ll consider adding that in a future update!

  • Ndeye Fatou Diop
    Ndeye Fatou DiopApr 7, 2025

    Very cool article! Exhaustive yet simple 🙏

  • Massimo Artizzu
    Massimo ArtizzuApr 9, 2025

    Ciao Soumaya! Can I suggest to turn this article into a series where you can go in-depth for each of these points? With more examples and explanations. 😊

    • Soumaya Erradi
      Soumaya ErradiApr 12, 2025

      I love that idea, thank you for the suggestion! 😊

Add comment