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

React hooks introductory tutorial

State Hooks Examples: import { useState } from &#...

Detailed explanation of HTML form elements (Part 2)

HTML Input Attributes The value attribute The val...

Analysis of the reasons why MySQL's index system uses B+ tree

Table of contents 1. What is an index? 2. Why do ...

Detailed explanation of CSS3 text shadow text-shadow property

Text shadow text-shadow property effects: 1. Lowe...

Implementation and optimization of MySql subquery IN

Table of contents Why is IN slow? Which is faster...

HTML+CSS+jQuery imitates the search hot list tab effect with screenshots

Copy code The code is as follows: <!DOCTYPE ht...

JavaScript exquisite snake implementation process

Table of contents 1. Create HTML structure 2. Cre...

MySQL compression usage scenarios and solutions

Introduction Describes the use cases and solution...

Pure CSS to achieve cloudy weather icon effect

Effect The effect is as follows ​ Implementation ...

MYSQL slow query and log settings and testing

1. Introduction By enabling the slow query log, M...

Building command line applications with JavaScript

Table of contents 1. Install node 2. Install Comm...

Example of how to implement embedded table with vue+elementUI

During my internship in my senior year, I encount...

Basic Implementation of AOP Programming in JavaScript

Introduction to AOP The main function of AOP (Asp...

Understanding Vuex in one article

Table of contents Overview Vuex four major object...