【TS】Basic data type

Like JS, TS divides all data types into "basic data types" and "reference data types".

1. Basic data types

number 
string
boolean
null
undefined
symbol
bigint

1.1 Basic types

When creating a variable, var, let, and const are used in the same way as JS, but the data type of the variable needs to be constrained.
Basic grammar:var 变量名: 类型 = 值 ;

var a: number = 1;
let b: string = 'typescript';
const c: boolean = true;
const e: undefined = undefined;
const f: null = null;

1.2 void

voidIndicates the absence of any data type (or the null value undefined).

Usually, when a function does not return a value, the type of the return value is set to void.

function fn(): void {
    
     ... };

1.3 never

neverNo value, never returns a result, used to return an error.

function fn(): never {
    
    
	throw new Error('报错!')
}

2. Special data types

2.1 any

anyRepresents any data type. (generally not recommended)

let d: any;(显示的any//声明变量如果不指定类型,则TS解析器会自动判断变量的类型为any(隐式的any) 。
let d;
d = 10;
d = "hello";
d = true;
//any 类型的变量可以赋值给任意类型的变量。

2.2 unknown

unknownA value of unknown type, like any

In fact, it is a type-safe any, but it cannot be directly assigned to other variables.

2.3 array array

By default, TS requires that each item in the array must be of the same data type.

两种写法:
const a: number[];
const b: Array<string>;

在声明时直接赋值
const a: number[] = [1,2,3];
const b: Array<string> = ['1','2','3'];

也可以将数组的类型设置为any
const c: any[] = [1,'2',3];

2.4 The original tuple

Allows multiple types of data to be stored in an array.

However, it is required that the values ​​and tuple types in the array must: 数量要一致 位置要对应 类型要对应.

const a: [number, string] = [18,'zhangsan'];

2.5 object object

When defining an object in TS, it is necessary to define which attributes are in the object and what type the value of each attribute is.

const user: {
    
     _id: number, name: string } = {
    
     id: 1, name: 'Li'};

// ? 表示可选属性
const user: {
    
     _id: number, name?: string } = {
    
     id: 1, name: 'Li'};

// [propName: string]: any 任意可选属性
const user: {
    
     _id: number, name: string, [propName: string]: any } 
= {
    
     id: 1, name: 'Li', age: 18, gender: '男'};

//特殊的 可以赋值一个函数,也不会报错
let a: object;
a:{
    
    };
a = function () {
    
    };

2.6 enumeration enum

enumThe enumeration type is used in scenarios where the value is limited to a certain range, such as only seven days in a week, and the colors are limited to red, green, and blue.

2.6.1 Numeric enumeration

The number enumeration has self-increment behavior, which can be used to 初始器specify the self-increment from a certain number.

(1) Do not use the initializer

//a 使用默认值为 0,其余成员会从 0 开始自动增长。
enum test {
    
    
    a,	// a = 0
    b,	// b = 1
    c,	// c = 2
    d,	// d = 3
};
Object.values(test).map((item)=>{
    
    
  console.log(item);
})

(2) Use the initializer

//a 使用默认值为 1,其余成员会从 1 开始自动增长。
enum test {
    
    
    a = 1,	// a = 1
    b,	// b = 2
    c,	// c = 3
    d,	// d = 4
};

2.6.2 String enumeration

String enumeration has no self-increment behavior, and each member must be initialized and assigned.

enum test {
    
    
    a = 'a',
    b = 'b',
    c = 'c',
    d = 'd',
}

2.6.3 Heterogeneous enumerations

Mixing string and number members is deprecated.

enum test {
    
    
    Yes = 'Y',
    No = 0,
}

Guess you like

Origin blog.csdn.net/qq_53931766/article/details/125314118