Skincare Tips For A Freshman

If unfortunately my glowing skin get ruined and I wanted to glow it back in 6 month, this is what I'd do. Cleanser don't just cleanse the surface alone, it penetrates into the skin pores to remove…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




3 ways to narrow object types with TypeScript

In the example above, neither line 6 nor 9 throw an error, because .swim() is unique to the Fish type and .fly() is unique to the Bird type.

These can be a bit more difficult to grasp. They can be especially handy when you find yourself checking the same type over and over again. This is a construct provided by TypeScript itself (rather than most others provided by JavaScript). It gives you more control by allowing you to define your own type guard. We’ll continue with the types we created above and define a type guard for them.

Notice pet is Fish in the return type of the type guarding function. This is the part that does all the magic, it’s what is known as the type predicate.

In line 5 we’re calling our type guard. Now TypeScript will know in the rest of the block scoped by “if” whether pet is of type Bird or Fish. Even though the return value of isFish() is not assigned, pet is still inferred correctly by TypeScript thanks to the line pet is Fish.

It’s common to see typings like this Shape type.

Sometimes this is exactly what you need. However, in this case we know that a circle can’t have a side length and we know that a square doesn’t have a radius. We’ll get undefined errors that may look like this one (maybe this is why you’re reading this article):

With the type Shape we defined above TypeScript can’t figure out if our shape is a circle or not. Here it makes sense to split them into their own types in order to stay away from the optional typings of radius and sindeLength. What is important is that the new types share a common key which can be used to distinguish the two. We’ll use the key kind. Our types will now look like this:

Now we can call the same function we did above and will not get the undefined errors because TypeScript can successfully infer what the type of the object will be.

These are three strategies you can use to narrow down your objects with TypeScript to prevent undefined errors and give you more type safety in your code.

Add a comment

Related posts:

Some Friends Will Be At The Tokyo Olympics

The athletes are ready. Some of them have been preparing for this great opportunity to participate in the Olympics for nearly their entire lives. They have trained well. Although disappointed when…

Why The Legends of King Arthur Will Never Die

The legends of King Arthur and the Knights of the Round Table have captured audiences for years. There has been a wide variety of interpretations in books, films, and TV shows, ranging from whimsical…

Do Different Personality Types Affect Relationship Longevity?

It has been said that opposites attract, Yin and Yang, you know…. a little difference in personality keeps things spiced up! Well… Is this really true? When it comes to romantic relationships, we all…