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 numberslet 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 Typesenum 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 typelet 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 Typefunction 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')) SummarizeThis 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:
|
<<: Detailed explanation of the difference between tinyint and int in MySQL
>>: Horizontal header menu implemented with CSS3
State Hooks Examples: import { useState } from ...
HTML Input Attributes The value attribute The val...
Table of contents 1. What is an index? 2. Why do ...
Text shadow text-shadow property effects: 1. Lowe...
Table of contents Why is IN slow? Which is faster...
Copy code The code is as follows: <!DOCTYPE ht...
Table of contents 1. Create HTML structure 2. Cre...
Introduction Describes the use cases and solution...
Effect The effect is as follows Implementation ...
1. Introduction By enabling the slow query log, M...
Table of contents 1. Install node 2. Install Comm...
During my internship in my senior year, I encount...
Introduction to AOP The main function of AOP (Asp...
This is what happened. Today I was playing with G...
Table of contents Overview Vuex four major object...