So far, the handbook has covered types which are atomic objects. However, as you model more types you find yourself looking for tools which let you compose or combine existing types instead of creating them from scratch. Show
Intersection and Union types are one of the ways in which you can compose types. Union TypesOccasionally, you’ll run into a library that expects a parameter to be either a
The problem with
In traditional object-oriented code, we might abstract over the two types by creating a
hierarchy of types. While this is much more explicit, it’s also a little bit overkill. One of the nice things about the original version of Instead of
A union type describes a value that can be
one of several types. We use the vertical bar ( Unions with Common FieldsIf we have a value that is a union type, we can only access members that are common to all types in the union.
Union types can be a bit tricky here, but it just takes a bit of intuition to get used to. If a value has the type Discriminating UnionsA common technique for working with unions is to have a single field which uses literal types which you can use to let TypeScript narrow down the possible current type. For example, we’re going to create a union of three types which have a single shared field.
All of the above types have a field named
Given the With
In this case, you can use a
Union Exhaustiveness checkingWe would like the compiler to tell us when we don’t cover all variants of the discriminated
union. For example, if we add
There are two ways to do this. The first is to turn on
Because the The second method uses the
Here, Intersection TypesIntersection types are closely related to union types, but they are used very
differently. An intersection type combines multiple types into one. This allows you to add together existing types to get a single type that has all the features you need. For example, For example, if you had networking requests with consistent error handling then you could separate out the error handling into its own type which is merged with types which correspond to a single response type.
When you code an if statement within another if statement The statements are?A nested if statement is an if statement placed inside another if statement. Nested if statements are often used when you must test a combination of conditions before deciding on the proper action.
Which of the following is typically used in a flowchart to indicate a decisions?The most common symbol used in a flowchart is the rectangle. A rectangle represents a process, operation, or a task. The next most common symbol is the diamond which is used to represent a decision.
Can a boolean variable hold any character?They can have only two possible values: either 0 or 1. You cannot add or subtract them. A Boolean variable is a special type of memory in a computer that can only store two values: true or false.
Which of the following data types can store the value 0 using the least amount of memory?The data type that can store the value 0 using least amount of memory is “ Byte ”. Hence, the correct answer is option “ D ”.
|