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 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 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 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 (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 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 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. 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 quick example of when we forgot to test the case for the man species.
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 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.
Click here to claim your Sponsored Listing.
Category
Address
Odessa, 65058
Настройка рекламы в социальных сетях с гарантией результата. Продвигаем малый и средний бизнес с 0 до результата.
Вулиця Мечникова, 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.
Вулиця Толбухіна, 37
Odessa, 65000
https://bridesbay.com/photos/1015078_1429026_wh8zk5_orig.jpg
CHernyakhovskogo Ulica , Bld. 12/Б, Appt. 32
Odessa
It is better to fail in originality than to succeed in imitation.