Typescript

Typescript 10 Quick Tips I Have Learned Using TypeScript

Readonly: Constructs a type with all properties of Type set to read-only, meaning the properties of the constructed type...
15/02/2023

Readonly: Constructs a type with all properties of Type set to read-only, meaning the properties of the constructed type cannot be reassigned.

You have learned about the Partial to construct a type with all properties of Type set to optional. But there are more o...
15/02/2023

You have learned about the Partial to construct a type with all properties of Type set to optional. But there are more of them. Here are the ones I often use:

Required: Constructs a type consisting of all properties of Type set to required. As you can guess, this is the opposite of Partial.

What if we would like to create a new type based on another type but with all its properties set to optional. How could ...
15/02/2023

What if we would like to create a new type based on another type but with all its properties set to optional. How could we do this? 🤔

TypeScript ships with a utility I use every week called Partial. Here is how it works.

As you can see in the example above, what is remarkable is that no matter the object’s shape, we can create another type...
15/02/2023

As you can see in the example above, what is remarkable is that no matter the object’s shape, we can create another type (Freeze) that will include all the properties set to read-only.

Here is another generic type that will set all properties of the object as non-read-only.

Generic types are handy. The more you get familiar with TypeScript, the more you use them. They allow your code to be mo...
15/02/2023

Generic types are handy. The more you get familiar with TypeScript, the more you use them. They allow your code to be more flexible by allowing you to set the type yourself. An example will paint a thousand words. 😃

Of course, there are also some subtle differences between both, as explained in this great video. For instance, an inter...
10/02/2023

Of course, there are also some subtle differences between both, as explained in this great video. For instance, an interface can extend other interfaces, while you need a union or an intersection to merge two types together (as they are static).

Also, from my experience, when I deal with types, the error message can usually be more brutal to understand when something goes wrong.

3. Interfaces vs. TypesWe commonly hear this question from people who just started their TypeScript journey: What is the...
10/02/2023

3. Interfaces vs. Types
We commonly hear this question from people who just started their TypeScript journey: What is the difference between an interface and a type? Should I use both? 🤔

It took me a few weeks, in the beginning, to set a rule for myself as we can often do the same thing with both of them. Here is how I would sum it up: When I work with classes or objects, I use an interface. When I am not, I use a type. It would help if you remembered that the most important thing is to be consistent with your choices inside your codebase.

We can also use this type to our advantage by ensuring every critical situation is handled in our functions. Here is a q...
10/02/2023

We can also use this type to our advantage by ensuring every critical situation is handled in our functions. Here is a quick example of when we forgot to test the case for the man species.

2. The Never Type Can Be Handy for Error HandlingThere is also another type we are never using when we start playing wit...
10/02/2023

2. The Never Type Can Be Handy for Error Handling
There is also another type we are never using when we start playing with TypeScript: the never keyword. In a nutshell, it represents the type of values that never occur.

A while back, I found that this keyword can be handy when writing functions that trigger one or multiple errors and never return anything.

The main difference between any and unknown is that the unknown type is only assignable to the any type and the unknown ...
10/02/2023

The main difference between any and unknown is that the unknown type is only assignable to the any type and the unknown type itself. Thus, it is a much less permissive type and an excellent substitute for any, as it will constantly remind your editor that you must replace it with something more explicit. By switching from any to unknown, we permit (almost) nothing instead of allowing everything.

Address

Odessa

Alerts

Be the first to know and let us send you an email when Typescript posts news and promotions. Your email address will not be used for any other purpose, and you can unsubscribe at any time.

Share