# TypeScript the Right Way

![rw-book-cover](https://i.ytimg.com/vi/UMEp6eFU16k/maxresdefault.jpg)

## Metadata
- Author: [[Awesome]]
- Full Title: TypeScript the Right Way
- Category: #articles
- Document Tags: [[dev]] [[dev/javascript]] 
- URL: https://youtube.com/watch?v=UMEp6eFU16k&si=5FrI-L7etAUGFl1K
- Archive: https://web-archive.alecodes.page/bookmarks?bf=1&search=&title=TypeScript%20the%20Right%20Way

> [!note]
> **Background:** I want to incorporate TypeScript in my projects to enhance code quality, readability, and maintainability while ensuring type safety.
> ### Top 3 Important Takeaways or To-Dos:
> 🛠️ **Keep Types Simple:** Avoid using nested types, complex unions, and intersections. Overcomplicated types can lead to confusion and higher maintenance costs, so aim to maintain simplicity in your type definitions. 
> 🔍 **Utilize Type Guards:** Implement type guards to check for null or undefined values before using variables. This practice enhances code safety and reliability by ensuring that variables are properly validated. 
> 🚫 **Avoid Using "Any":** Steer clear of the "any" type, as it defeats the purpose of type safety in TypeScript. Instead, use union types, type guards, or the unknown type to handle uncertain variable types effectively.
> [!tldr]
> TypeScript has become popular for modern web development because it improves upon JavaScript's weaknesses. It adds static type checking, helping developers catch errors before their code runs. Following best practices in TypeScript, such as avoiding overly complex types, ensures safer and more maintainable code.

## Highlights
Let’s say you have a basic function that adds up  two numbers in a main.js file. In pure JavaScript   there isn’t anything stopping you from passing  strings or any other type of object as a parameter   to this function. To avoid this, we need that  extra layer of validation provided by TypeScript. [View Highlight](https://read.readwise.io/read/01j7r04ptybp6b904wk49qar0a))

the first rule should be pretty obvious -  avoid using the “any” type at all costs. [View Highlight](https://read.readwise.io/read/01j7r07sqy58gkbmcg2n0kg1tv))

there will be those corner case scenarios where  you don’t really know the shape of an object ... In such scenarios don’t hesitate to use union  types and type guards to make your code safer. If you are truly unsure of a variable's type,  you could use the unknown type instead of any or   leave out the type reference to allow TypeScript  to infer it.

remember to avoid overly verbose type  annotations when they are unnecessary. [View Highlight](https://read.readwise.io/read/01j7r0c9r7ws816nrtcmbksehf))

TypeScript offers a strict mode compiler  option that enforces more rigorous type   checking and constraints to enhance code  quality and to catch potential issues during   development. You can enable this by setting  the strict flag to true in the TS config file. [View Highlight](https://read.readwise.io/read/01j7r0dn8e90zkxvdj370t22tw))

JavaScript’s strict mode was  introduced in ES 5 and you should always use   it as well. This will allow you to opt in to a  restricted variant of JavaScript where silent   errors are changed to throw errors, some mistakes  that make it difficult for the engine to perform   optimizations are fixed, and syntax likely to  be defined in future versions is prohibited. [View Highlight](https://read.readwise.io/read/01j7r0f8r2nskx1jagx6sn7hsf))

A type alias creates a new name for a type.  This can be a primitive, union, intersection,   tuple or any other type. In other words  it can represent complex structures and   give you flexibility in defining types  that go beyond simple object shapes. On the other hand, interfaces are  primarily used to define the structure   or shape of an object or class. Interfaces  in TypeScript are more focused on describing   the properties and methods  that an object should have,  
and they provide a powerful way to enforce  the structure of objects across your codebase. [View Highlight](https://read.readwise.io/read/01j7r0hfyz8nps371dqga3bmmh))

you should use interfaces for object  shapes and class contracts, use types for complex   and flexible structures and use a combination  of both to create powerful type definitions. [View Highlight](https://read.readwise.io/read/01j7r0jedxp5gd3h5qpkr428v0))

Non-Nullable assertions are Another big source of   potential problems since they can lead  to runtime errors if used incorrectly. ... you should use type guards  to ensure that variables are properly checked   for null or undefined values before use. This  approach leads to safer and more reliable code.

keep things simple.
Overcomplicated types are a common pitfall  in TypeScript projects, and although complex   types may occasionally be needed, making them  overly complicated can result in confusion,   reduced readability, and higher maintenance costs. [View Highlight](https://read.readwise.io/read/01j7r0pctvv439x5ev9w1ccz74))

Avoid nested types since they are hard  to read and maintain, avoid complex union   and intersection types since they can lead to  convoluted and difficult-to-understand types,   and don’t overuse mapped and conditional types  since they can easily get over complicated. [View Highlight](https://read.readwise.io/read/01j7r0rw8djwydvfy9jdx0e8wj))

