Detailed explanation of TypeScript's basic types

Detailed explanation of TypeScript's basic types

Boolean Type

 // Boolean type ---> boolean
// let variable name: data type = value let flag: boolean = true;
console.log(flag)

Number Types

 //Number type--->number
let a1: number = 10 // decimal let a2: number = 0b1010 // binary let a3: number = 0o12 // octal let a4: number = 0xa // hexadecimal console.log(a1 + a2 + a3 + a4)

String Type

 // string type ---> string
let str1: string = 'The moon shines brightly before the bed';
let str2: string = 'Two pairs of shoes on the ground';
console.log(str1 + ',' + str2)

Concatenate strings and numbers

 let str3: string = 'My current age:'
let a5: number = 24
console.log(`${str3}${a5}`)

Summary: What type of variable is initially in ts? When assigning values ​​later, only data of this type can be used. It is not allowed to assign data of other types to the current variable.

undefined and null

 // Both undefined and null can be used as subclasses of other types, assigning undefined and null to variables of other types, such as: number type variable let und: undefined = undefined
let n1l: null = null
console.log(und)
console.log(n1l)

Array Types

 // Method 1: let variable name: data type [] = [value 1, value 2, value 3, ...]
let arr1: number[] = [10, 20, 30, 40, 50]
console.log(arr1);
 // Method 2: Generic writing // Syntax: let variable name: Array<data type>=[value1, value2, value3]
let arr2: Array<number> = [100, 200, 300]
console.log(arr2);

Note: After the array is defined, the data type inside must be consistent with the type when the array is defined, otherwise there will be an error message and it will not compile.

Tuple Types

 // Tuple type: When defining an array, the type and number of data are limited from the beginning. let arr3: [string, number, boolean] = ['小甜甜', 100, true];
console.log(arr3)
// Note: When using tuple type, the data type, position and number of data should be consistent with the data type and position when defining the tuple console.log(arr3[0].split(''));
console.log(arr3[1].toFixed(2));

Enumeration Types

 enum Color {
       red,
       green,
       blue
}
// Define a variable of the Color enumeration type to receive the enumeration value let color: Color = Color.red
console.log(color);
console.log(Color[2])

any type

 let str5: any = 100;
str5 = 'Uchiha Obito'
console.log(str5);
// When an array is to store multiple data with uncertain number and type, you can also use the any type to define the array let arr6: any = [100, 'Uchiha Obito', true];
console.log(arr6)
// In this case, there is no error message. The any type has advantages and disadvantages. console.log(arr6[1].split(''));

void Type

 function getobj(obj: object): object {
       console.log(obj);
       return {
           name: 'Kakashi',
           age: 27
       }
}
console.log(getobj({ name: 'Sasuke', age: 20 }))

Union Types

 // Requirement 1: Define a function to get the string value of a number or string value function getString(str: number | string): string {
      return str.toString();
}
console.log(getString('萨给'))
 
// Requirement 2: Define a function to get the length of a number or string value function getString1(str: number | string): number {
      return str.toString().length
      if ((<string>str).length) {
          return (str as string).length
      } else {
          return str.toString().length
      }
}
  console.log(getString1(12345))
  console.log(getString1('12345'))

Summarize

This article ends here. I hope it can be helpful to you. I also hope you can pay more attention to more content on 123WORDPRESS.COM!

You may also be interested in:
  • Explanation of TypeScript common types and application examples
  • Teach you to use typescript types to calculate Fibonacci
  • TypeScript Mapping Type Details
  • TypeScript Enumeration Type
  • Introduction to TypeScript basic types
  • Let's learn TypeScript types together

<<:  Detailed explanation of the difference between tinyint and int in MySQL

>>:  Horizontal header menu implemented with CSS3

Recommend

Solve the problem of installing Theano on Ubuntu 19

Solution: Directly in the directory where you dow...

hr horizontal line style example code

Copy code The code is as follows: <hr style=&q...

Detailed explanation of MySQL sql99 syntax inner join and non-equivalent join

#Case: Query employee salary levels SELECT salary...

Steps to export the fields and related attributes of MySQL tables

Need to export the fields and properties of the t...

JavaScript deshaking and throttling examples

Table of contents Stabilization Throttling: Anti-...

Summary of MySQL commonly used type conversion functions (recommended)

1. Concat function. Commonly used connection stri...

Several ways to use v-bind binding with Class and Style in Vue

Adding/removing classes to elements is a very com...

Summary of MySQL 8.0 memory-related parameters

Theoretically, the memory used by MySQL = global ...

HTML end tag issue and w3c standard

According to the principles of W3C, each start tag...

How to build a DHCP server in Linux

Table of contents 1. Basic knowledge: 2. DHCP ser...

Steps to deploy Docker project in IDEA

Now most projects have begun to be deployed on Do...

Docker uses the nsenter tool to enter the container

When using Docker containers, it is more convenie...