Variable declaration and basic data type summary in

Table of contents

1. Variable declaration

2. Basic data types

1.boolean(Boolean value)

2.number(number)

3.string(string)

4.array(array)

5.Tuple(tuple)

6.enum (enumeration)

7.Any

8.Void

9.Null and Undefined

10.Never

11.Object

3. Type aliases and union types


1. Variable declaration

        Because TypeScript (an open source programming language developed by Microsoft) is a superset of JavaScript, it supports the methods of declaring variables in JavaScript (var, let, const)

        The difference between declaring variables in TypeScript and JavaScript is that when declaring variables in JavaScript, the variable has no determined type, which means that we can assign all data types to the same variable name. TypeScript variables have clear types. When declaring a variable, we specify the data type for it at the same time. The declaration is as follows:

let a:boolean=true;
let b:number=111111111;
let c:string="这是字符串";

2. Basic data types

        JavaScript数据类型:         number boolean string array undefined  null

        TypeScript new data type:  tuple enum any void never

1.boolean(Boolean value)

      boolean 布尔值: false/true;

      How to declare a Boolean variable:


/* boolean布尔值 */
let a:boolean=true;
let b:boolean=false;
console.log(a,b);//true false

2.number(number)

        number value:Floating point number --------Supports 2.8.10.16 hexadecimal numbers and will be compiled into decimal numbers

        How to declare a numeric variable:

/* 数字 */
let num1:number=10;
let num2:number=0xf00d;//16进制
let num3:number=0b1010;//2进制
let num4:number=0o744;//8进制
let num5:number=NaN;
let num6:number=Infinity;
console.log(num1,num2,num3,num4,num5,num6);
// 10 61453 10 484 NaN Infinity

3.string(string)

        string string:Double quotation mark"" Single quotation mark'' Backtick mark `` text data enclosed

   How to declare a string variable:

/* 字符串 */
let str1:string="字符串1";
let str2:string="字符串2";
let name1:string="大聪明";
let age:number=21;
let introduce:string=`我的名字是${name1},我的年龄是${age}`
console.log(str1,str2);//字符串1 字符串2
console.log(introduce);//我的名字是大聪明,我的年龄是21

4.array(array)

        There are two ways to declare array:

                1. You can add [] after the element type to indicate an array composed of elements of this type

                2.Array<Element Type>

 How to declare array variables:

/* 数组 */
let arr1:number[]=[1,2,3];
let arr2:string[]=["a","b","c"]; 
let arr3:Array<number>=[1,2,3]
let arr4:Array<string>=["a","b","c"]
let arr5:Array<number|string>=["a",1]//表示数组中的数据既可以是数字也可以是字符串类型
console.log(arr1);//[ 1, 2, 3 ]
console.log(arr2);//[ 'a', 'b', 'c' ]
console.log(arr3);//[ 1, 2, 3 ]
console.log(arr4);//[ 'a', 'b', 'c' ]
console.log(arr5);//[ 'a', 1 ]

5.Tuple(tuple)

        Tuple: represents an array with a known number and type of elements, and the types of each element do not have to be the same

/* 元组 */
let a:[string,number]=["string1",1111];
/* 
类似解构赋值实现
a[0]:string="atring1"
a[1]:number=1111
*/
console.log(a[0]);//string1
console.log(a[1]);//1111

6.enum (enumeration)

        Enum type - Like C# and other languages, enumeration types can be used to give friendly names to a set of values.

/* 枚举 */
enum Color {Red, Green, Blue}
//等同于enum Color {Red=0, Green=1, Blue=2}
let c: Color = Color.Green;
console.log(c);//1

          By default, elements are numbered starting from 0.

          certainly! You can also manually specify member values

/* 枚举 */
enum Color {Red=1, Green, Blue}
// 等同于:enum Color {Red=1, Green=2, Blue=3} 逐个递增
let c: Color = Color.Green;
console.log(c);//2
/* 枚举 */
enum Color {Red=1, Green=4, Blue=5}
// 等同于:enum Color {Red=1, Green=4, Blue=5}
let c: Color = Color.Green;
console.log(c);//4

        One convenience provided by enumeration types is that you can get its name from the enumeration value:

/* 枚举 */
enum Color {Red=1, Green=4, Blue=5}
// 等同于:enum Color {Red=1, Green=4, Blue=5}
let c: Color = Color.Green;
console.log(c);//4

let colorName: string = Color[4];//4表示枚举的值
console.log(colorName);//显示'Green'因为上面代码里它的值是4

7.Any

        Sometimes, we will want to specify a type for variables whose type is not clear at the programming stage . These values ​​may come from dynamic content, such as from user input or third-party code libraries. In this case, we don't want the type checker to check these values ​​and just let them pass the compile-time check. Then we can use anytype to mark these variables:

/* any */
let notSure1: any = 4;
let notSure2: any = "a";
let notSure3: any = true;
let notSure4: any = [1];
console.log(notSure1,notSure2,notSure3,notSure4);//4 a true [ 1 ]

        Note: After declaring a variable to be an arbitrary value, any operation on it will return an arbitrary value.​  

8.Void

        In a way, the void type is like the opposite of the any type, which means there is no type at all. When a function does not return a value, you will usually see that its return value type is void:

/* void */
function fn(): void {
    console.log("1111111111111111");
}

        ​​​​​Note: It is useless to declare a variable of type void. You can assign undefined to void, but you cannot assign null to void.

9.Null and Undefined

  undefined and null each have their own types called undefined and null respectively. Similar to void, their types are not very useful

10.Never

  neverThe type represents the type of values ​​that never exist. For example, never types are those that always throw an exception or not at all Function expression that will not return a valueorReturn value type of arrow function expression

        never type is a subtype of any type and can be assigned to any type; however, does not type is (in addition to itself). Even  cannot be assigned to . neverneverneveranynever

Note: The function that returns never must have an unreachable end point

function fn(): never {
    while (true) {
    }
}

function error(message: string): never {
    throw new Error(message);
}

11.Object

  object represents a non-primitive type, that is, except number, string, boolean, symbol, null or undefined. The difference between object and Object is that Object is a class (Object is the base class (parent class) of all classes), and object is a reference type

3. Type aliases and union types

Union of multiple types, the relationship between them is or .

type manyType = number | boolean | string;
let a: manyType
a = 1;
console.log(a);//1
a = true;
console.log(a);//true
a = 'a';
console.log(a);//a

4. Type inference

        After a variable without a declared type is assigned once, the type of the variable is determined, and the type cannot be changed when assigning again.

Guess you like

Origin blog.csdn.net/m0_48465196/article/details/128457456