TypeScript union types, intersection types and type guards

TypeScript union types, intersection types and type guards

1. Union Type

The so-called union type is to define some types. The defined variables only need to satisfy any one type. The union type is defined using |. The sample code is as follows:

// Define the union type through the | symbol let value: number | boolean | string = '一碗周'
value = 18

In the above code we define a value variable, which can be a number, boolean or string type.

2. Crossover Type

We introduce union types, and then we look at intersection types, which are very similar to union types.

The so-called cross type needs to satisfy all types, and the cross type is defined using the & symbol.

The sample code is as follows:

// Define three common interface types interface Name {
  name: string
}
interface Age {
  age: number
}
interface Hobby {
  hobby: string
}
// Define an object that is a union type of the above three objects let person: Name & Age & Hobby = {
  // If one type is not allocated, an exception will be thrown name: '一碗周',
  age: 18,
  hobby: 'coding',
}

3. Type protection

Now we have a requirement: get the first number in an array of any type.

The implementation code is as follows:

// Define an array containing number or string const arr: (number | string)[] = [1, 'number']
// Traverse the array and return the first number const getValue: (arr: (number | string)[]) => number = (
  arr: (number | string)[],
): number => {
  for (let i = 0; i < arr.length; i++) {
    // If the current value is not a NaN when converted to a number, return if (!isNaN(Number(arr[i]))) {
      return arr[i] // Type 'string | number' cannot be assigned to type 'number'.
    }
  }
}


In the above code, when return , it is not known whether the returned value is of type number . So an exception will be thrown.

The above functions can be accomplished through type assertions. The sample code is as follows:

const getValue: (arr: (number | string)[]) => number = (
  arr: (number | string)[],
): number => {
  for (let i = 0; i < arr.length; i++) {
    // If the current value is not a NaN when converted to a number, return if (!isNaN(Number(arr[i]))) {
      return arr[i] as number // Tell the compiler that I am returning a number
    }
  }
}

What is a type assertion? Please refer to: Type assertion

If you use type assertions to explain, it will be more cumbersome if the desired data type is different. At this time, type protection is needed to complete this function.

There are three main types of type protection:

3.1 Custom Type Protection

The way to define custom type protection is to define a function whose return structure is in the form of parameterName is type , which is a type predicate. parameterName must be a parameter name from the current function parameters.

The sample code is as follows:

// Use custom type protection // 1. Define a function whose return value is a type predicate, that is, parameterName is Type, which is the form of parameter name is type function isNumber(value: number | string): value is number {
  // If true is returned, it means the value passed in is the type after is
  return !isNaN(Number(value))
}
// 2. Define a function to get a number const getNumber: (value: number | string) => number = (
  value: number | string,
): number => {
  // If the value of calling isNumber is true, it means value is a number, so return the number if (isNumber(value)) {
    return value
  }
}
// 3. Call to get the final value const getValue: (arr: (number | string)[]) => number = (
  arr: (number | string)[],
): number => {
  for (let i = 0; i < arr.length; i++) {
    // If a number is returned, convert it to a boolean value of true
    if (getNumber(arr[i]) || getNumber(arr[i]) === 0) {
      return getNumber(arr[i])
    }
  }
}

The reason for defining the second function is that there are still problems in directly using i as the return value in the array, so a function is defined as a transition.

3.2typeof type protection

The typeof keyword in JavaScript can determine the current type, but it can only determine four types: number , string , boolean , and symbol .

This is enough for this requirement. Next, let's see how to implement type protection through typeof .

The sample code is as follows:

// 1. Define a function to get a number const getNumber: (value: number | string) => number = (
  value: number | string,
): number => {
  // Check if the current value is a string, if so return the current value if (typeof value === 'number') {
    return value
  }
}
// 2. Call to get the final value const getValue: (arr: (number | string)[]) => number = (
  arr: (number | string)[],
): number => {
  for (let i = 0; i < arr.length; i++) {
    // If a number is returned, convert it to a boolean value of true
    if (getNumber(arr[i]) || getNumber(arr[i]) === 0) {
      return getNumber(arr[i])
    }
  }
}

3.3instanceof type protection

instanceof operator is also a native operator provided in JavaScript . It is used to determine whether an instance is created by a constructor or a class using ES6 syntax. In TypeScript, type protection can also be achieved through instanceof operator.

The sample code is as follows:

/**
 * Since instanceof only supports reference types and not primitive types, we need to make some changes here and modify the array to the following:
 */
const arr2: (Number | String)[] = [new String('彼岸繁华'), new Number(10)]
// 1. Define a function to get a number const getNumber: (value) => number = (value): number => {
  // Determine whether the current value is of Number type and convert the current value into a string and return if (value instanceof Number) {
    return Number(value)
  }
}
// 2. Call to get the final value const getValue: (arr: (Number | String)[]) => number = (
  arr: (Number | String)[],
): number => {
  for (let i = 0; i < arr.length; i++) {
    // If a number is returned, convert it to a boolean value of true
    if (getNumber(arr[i]) || getNumber(arr[i]) === 0) {
      return getNumber(arr[i])
    }
  }
}


There are two points to note when using instanceof:

  • Only applicable to any reference type, not primitive types.
  • Whether the prototype chain of the former contains the prototype object of the latter.

This concludes this article on TypeScript union types, intersection types, and type protection. For more information about TypeScript union types, intersection types, and type protection, please search previous articles on 123WORDPRESS.COM or continue browsing the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • TypeScript union types, intersection types and type guards
  • Detailed explanation of TypeScript 2.0 marked union types
  • A brief discussion on TypeScript's type protection mechanism

<<:  Basic usage of @Font-face and how to make it compatible with all browsers

>>:  Use HTML to write a simple email template

Recommend

Specific use of stacking context in CSS

Preface Under the influence of some CSS interacti...

A solution to the abnormal exit of Tomcat caused by semaphore

I'm playing with big data recently. A friend ...

Detailed explanation of COLLATION examples in MySQL that you may have overlooked

Preface The string types of MySQL database are CH...

Analysis and solution of flex layout collapse caused by Chrome 73

Phenomenon There are several nested flex structur...

Web Theory: Don't make me think Reading Notes

Chapter 1 <br />The most important principl...

How to install MySQL server community version MySQL 5.7.22 winx64 in win10

Download: http://dev.mysql.com/downloads/mysql/ U...

Java example code to generate random characters

Sample code: import java.util.Random; import java...

The best solution for resetting the root password of MySQL 8.0.23

This method was edited on February 7, 2021. The v...

How to modify mysql to allow remote connections

Regarding the issue of MySQL remote connection, w...

Centos6.9 installation Mysql5.7.18 step record

Installation sequence rpm -ivh mysql-community-co...

Detailed process of installing the docker plugin in IntelliJ IDEA (2018 version)

Table of contents 1. Development Environment 2. I...

Example of adding music video to HTML page

1. Video tag Supports automatic playback in Firef...