# 数据类型

## 基本数据类型

### `number`

```typescript
let n1: number = 10;
let n2 = 10.3;
// typescript 中，number数字类型实际上都是浮点数

// 除了支持十进制表示，还支持二进制、八进制、十六进制表示
let decLiteral: number = 2023;
let binaryLiteral: number = 0b11111100111;
let octalLiteral: number = 0o3747;
let hexLiteral: number = 0x7e7;
```

### `boolean`

```typescript
let b1: boolean = true;
let b2 = true;
```

### `string`

```typescript
// 单引号和双引号都表示字符串
let s1: string = "hello";
let s2: string = 'hello';
```

### `any`

```typescript
// 任意类型，什么值都可以传入
let c: any;
c = "hello";
c = 1;

// any类型的值可以传递给任意类型，因为any的意思是任意类型，可以是任何类型
let d: string = c;  // 将any类型的c，实际是number类型，传递给一个string类型的d变量不会报错，但是后续操作会出现异常

// 不建议使用any类型
```

### `unknow`

我们可能想要在编程阶段给还不清楚是什么类型的变量指定一个类型，那就可以使用`unkown`类型来标记这些变量。

```typescript
// 未知类型，与any表现类似
let e: unknow;
e = 10;
e = true;

// 但是unknow
let f: boolean;
// f = e; // 虽然他俩的值都是布尔类型，但是未知类型的变量不能赋予其他类型

// 解决办法，进行js的类型判断
// 如果是boolean类型才进行赋值
if (typeof e === 'boolean') {
  f = e;
}

// 或者进行ts的类型断言，告诉解析器此类型就是boolean
// 下面两种写法表达的含义相同
f = e as boolean;
f = <boolean>e;
```

### `void`

设置函数没有返回值：

```typescript
function test(): void { 
}
```

### `never`

`never`代表函数永远不会有返回，比较常见的情况有：

* 函数内部有`while(true) {}`，函数永远不会走完，比如http服务器
* 函数不会走完，总是抛出异常，`function foo() { throw new Error('Not Implemented') }`

这些函数可以定义为`never`

### `null`和`undefined`

在Typescript中，`undefined`和`null`两者各自拥有自己的类型，叫做`undefined`和`null`。

```typescript
let nu: null = null
let udf: undefined = undefined;
```

### `联合类型`

```typescript
// 变量fn可以是string，也可以是number
let fn : string | number;
fn = 'nine';
fn = 9;
```

### `字面量`

```typescript
let sex: "male" | "female";
// sex 变量的值，只能是 male字符串或者 female字符串

let count : 10;
// count 变量的值只能赋予10，类似常量
```

## 复合数据类型

### `object`

```typescript
let a: object

a = {}
// a = 10; // 报错
a = function () {
    console.log("hello world");
};
```

#### 对象规则限制

```typescript
// 对对象规则限制
let b: { name: string, age: number }; // 变量b是一个对象，并且结构必须为 {name:string, age: number}
b = {name: "张三", age: 12}; // 正确
// b = {name: "李四"} // 错误，缺少age字段
// b = {name: "李四", age: 12, hobby: "打篮球"} // 错误，多了hobby字段
```

#### 可选的规则

在字段后增加`?`代表字段是可选的

```typescript
// 对对象规则限制
let b: { name: string, age?: number }; // 变量b是一个对象，age属性是可选的
b = {name: "张三", age: 12}; // 正确
b = {name: "李四"} // 正确，age字段是可选的
// b = {name: "李四", age: 12, hobby: "打篮球"} // 错误，多了hobby字段
```

#### 最小的规则

只要求有某些字段，可以使用任意属性：

```typescript
// 对对象规则限制
let b: { name: string, [propName: string]: any }; // 变量b是一个对象，并且结构必须为 {name:string, [propName: string]: any}
b = {name: "李四"} // 正确
b = {name: "李四", age: 12, hobby: "打篮球"} // 正确，只要有name属性即可

// [propName: string]: any 代表多个任意类型的属性
```

### `array`数组

```typescript
// 单一类型数组
let arr: number[] = [1, 2, 3];

// 多类型数组
let arr2: (number | string)[] = [1, "2", 3]

// 使用对象+泛型创建数组
let arr3: Array<number> = [1, 2, 3];
```

### `tuple`元组

元组（tuple）是关系数据库中的基本概念，关系是一张表，表中的每行（即数据库中的每条记录）就是一个元组，每列就是一个属性。 在二维表里，元组也称为行。

元组的长度，类型等信息都是固定的，但是元组中的每个元素类型可以是不同的：

```typescript
let t: [number, string, number, string]; // 人的元组
t = [1, "张三", 18, "打篮球"]
```

### `enum`枚举

```typescript
enum Gender { // 是一个类
    Male,
    FeMal
}


let g: Gender = Gender.Male;

// 判断枚举类型
if (g === Gender.Male) {
    
}
```

## 类型别名

```typescript
type myType = 1 | 2 | 3 | 4;
let k: myType;
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://yangsx95.gitbook.io/notes/programming-language/typescript/shu-ju-lei-xing.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
