Typescript

Typescript

10 Quick Tips I Have Learned Using TypeScript

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.

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.

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.

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.

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. 😃

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.

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.

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.

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.

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.

Want your business to be the top-listed Advertising & Marketing Company in Odessa?
Click here to claim your Sponsored Listing.

Address

Odessa

Other Social Media Agencies in Odessa (show all)
55 Vitalia Parkhomenko 55 Vitalia Parkhomenko
Генерала Петрова, 50
Odessa, 65008

Twinkle OQ Twinkle OQ
Odessa, 1451

Giggle Giggle
Базовая 12
Odessa, 65125

Одежда

RecrutCar131 RecrutCar131
Odessa

MAGNET Target - студия рекламного трафика MAGNET Target - студия рекламного трафика
Odessa, 65058

Настройка рекламы в социальных сетях с гарантией результата. Продвигаем малый и средний бизнес с 0 до результата.

Pumpkin F Pumpkin F
Вулиця Мечникова, 76, Этаж 1
Odessa, 65000

We have offices in the US, UK, Germany and Israel. We believe that excellence is our mission and a quality service is our value.

Yesvent Yesvent
Маяковского 3
Odessa, 17407

Yesvent

Honest month Honest month
Вулиця Толбухіна, 37
Odessa, 65000

https://bridesbay.com/photos/1015078_1429026_wh8zk5_orig.jpg

Present Lot Present Lot
Odessa, 23422

Present Lot

Slepez remark Slepez remark
Одесса
Odessa, 58545

Твердое сознание долга есть венец характера.

Augmented L Augmented L
CHernyakhovskogo Ulica , Bld. 12/Б, Appt. 32
Odessa

It is better to fail in originality than to succeed in imitation.

Super Bonus1 Super Bonus1
Odessa, 32432

Zahodi Suda