Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Clean Code with TypeScript
  • Table Of Contents Toc
Clean Code with TypeScript

Clean Code with TypeScript

By : Rukevwe Ojigbo, Dr. Sanjay Krishna Anbalagan
close
close
Clean Code with TypeScript

Clean Code with TypeScript

By: Rukevwe Ojigbo, Dr. Sanjay Krishna Anbalagan

Overview of this book

Clean Code with TypeScript is a practical guide to writing maintainable, efficient, and elegant TypeScript code. It equips developers with the essential principles and techniques to produce code that is both functional and easy to read and maintain. Written by Rukevwe Ojigbo and Dr. Sanjay Krishna Anbalagan, expert software engineers with extensive experience in building scalable, high-performance applications across industries, this book reflects practical lessons from their real-world projects. Throughout the book, you’ll work through hands-on implementations, including an LLM integration project and a full-stack TypeScript application, ensuring the concepts are grounded, relevant, and applicable to real development environments. What sets this book apart is its example-driven approach rooted in real-world scenarios. It goes beyond TypeScript best practices by developing your architectural thinking, enhancing team collaboration, and fostering long-term code quality. Whether you're new to TypeScript or an experienced developer, this guide will improve your TypeScript programming skills and help you deliver cleaner, more robust code. *Email sign-up and proof of purchase required
Table of Contents (16 chapters)
close
close
14
Other Books You May Enjoy
15
Index

Understanding conditional types in TypeScript

Conditional types in TypeScript allow us to create types that adapt based on other types. They're like chameleons for our type system. This allows you to create more flexible and adaptable type definitions in your code.

The basic idea is this: you define a type with a condition. If the condition is true, the type resolves to one type; if the condition is false, it resolves to a different type.

The basic syntax of a conditional type is as follows:

type MyConditionalType<T> = T extends U ? X : Y;

Let's break down the code:

  • T: This is like a placeholder for any type of data
  • extends U: This checks whether T is the same type as U (or a subtype of U)
  • X: If the check is true, the type becomes X
  • Y: If the check is false, the type becomes Y

Conditional types are commonly used in scenarios where the resulting type depends on the characteristics of the input type.

Let's look at an example.

Let's say we have an Animal type that can be either 'cat' or 'dog':

type Animal = 'cat' | 'dog';

Now, we create a Sound type that represents the sound each animal makes. If it's a cat, the sound is 'meow'; if it's a dog, the sound is 'woof':

type Sound<T extends Animal> = T extends 'cat' ? 'meow' : 'woof';

Let's test our Sound type:

let sound1: Sound<'cat'>; // sound1 is 'meow' (because 'cat' extends 'cat')
let sound2: Sound<'dog'>; // sound2 is 'woof' (because 'dog' doesn't extend 'ca ')

In this example, Sound is a conditional type. It checks whether the Animal type (T) is 'cat'. If it is, the sound becomes 'meow'. Otherwise, it becomes 'woof'.

In the next section, we are going over the tsconfig.json file, what it is, and how it impacts your project.

CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Clean Code with TypeScript
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist download Download options font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon