TypeScript basic data types and variable declarations

1. The data types contained in TypeScript:

1.any: any type, a variable declared as any can be assigned any type of value.

2.number: Number type, double-precision 64-bit floating point value. It can be used to represent integers and fractions.

3. string: string type.

4. boolean: Boolean type.

5. Array type: Declare the variable as an array.

let arr: number[] = [1, 2];
console.log(arr)
let arr2: Array<number> = [1,2];
console.log(arr2) //[ 1, 2 ]
let arr3: Array<string> = ["1","2"];
console.log(arr3) //[ '1', '2' ]
let arr4: Array<any> = ["1",2];
console.log(arr4) //[ '1', 2 ]

6. Tuple: The tuple type is used to represent an array with a known number and type of elements. The types of each element do not have to be the same, and the types of the corresponding positions need to be the same.

let yz: [string, number]; //元组
yz = ['yz', 1]; 
console.log(yz) //[ 'yz', 1 ]

7.enum: The enumeration type is used to define a collection of values. Each value contained in the enumeration type is generally used to manage multiple constants of the same series for status judgment.

enum Gender {Man = "男", Woman = "女"};
let m: Gender = Gender.Man;
let w: Gender = Gender.Woman;

console.log(m,w);    // 输出 男 女

Note: Gender can be used to judge the gender type to improve the readability and maintainability of the code. 

8. void: Used to identify the type of return value of the method, indicating that the method has no return value.

function hello(): void {
    alert("Hello World");
}

9.null: Indicates that the object value is missing, and null is a special type with only one value. Represents a null object reference. Use typeof to detect null and return an object.

let oneNull:null = null;
console.log(oneNull); //null
console.log(typeof oneNull); //object

10.undefined: Used to initialize variables to an undefined value.

let oneUndefined:undefined;
console.log(oneUndefined); //undefined
console.log(typeof oneUndefined); //undefined

11.never: It is a subtype of other types (including null and undefined) that represents values ​​that never appear. A variable declared as never type can only be assigned a value of never type. In a function, it usually throws an exception or cannot execute to a termination point (such as an infinite loop).

let oneNever: never;
oneNever = (()=>{ throw new Error('oneNever')})();
function oneNever(): never {
    while (true) {}
}

Note: Null and Undefined are subtypes of any other type (including void), and can be assigned to other types, such as numeric types. At this time, the assigned type will become null or undefined. (If strictNullChecks is not enabled, otherwise it can only be assigned to void or its corresponding type)

2. TypeScript variable declaration:

1. Variable declaration:

let [变量名] : [类型] = 值;
let oneName : string = "xiaoguai";

2. Type assertion: can be used to manually specify the type of a value

// <类型>值
// 值 as 类型
let hello:string = "Hello World!";
let str:string = "1";
// let str2:number =  <number> <any> str
let str2:number =   str as any 

console.log(str2)//"Hello World!"

console.log(typeof str2)  //string

Note: A can be successfully asserted as B when type A is a subset of type B, or type B is a subset of type A.

3. Type inference: When the type is not given, the TypeScript compiler uses type inference to infer the type. If the type cannot be inferred due to lack of declaration, then its type is taken to be the default dynamic any type.

4. Variable scope:

(1) Global Scope − A global variable is defined outside the program structure and it can be used anywhere in your code.

(2) Class scope − This variable can also be called  a field . Class variables are declared inside a class, but outside the methods of the class. This variable can be accessed through the object of the class. Class variables can also be static, and static variables can be accessed directly through the class name.

(3) Local scope − Local variables, local variables can only be used in a code block (such as: method) where it is declared.

If you have any questions, please comment below and I will answer them for you.

Guess you like

Origin blog.csdn.net/samxiaoguai/article/details/128228793