# 基础类型

| 类型               | 示例                                                                                 | 描述                                                                                                   |
| ---------------- | ---------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------- |
| 布尔值              | const areYouOk:boolean = true;                                                     |                                                                                                      |
| 数字               | const age:number = 123;                                                            |                                                                                                      |
| 字符串              | const myName: string = 'song';                                                     |                                                                                                      |
| 数组 (1)           | <p>const list: number\[] = \[1,2,4]; <br> const list: Array = \[1,2,4]</p>         |                                                                                                      |
| 元组               | <p>let x: \[string, number]; <br> x = \['1',1]</p>                                 | 表示一个已知元素数量和类型的数组，元素类型不必相同                                                                            |
| 枚举 (2)           | <p>enum Color { Red, Green, Blue } <br> let c: Color = Color.Green;</p>            |                                                                                                      |
| Any (3)          | <p>let notSure: any = 4; <br> notSure = "maybe a string"; <br>notSure = false;</p> | 不希望类型检查器进行检查                                                                                         |
| void (4)         |                                                                                    | 用处不大                                                                                                 |
| null 和 undefined |                                                                                    | 用处不大                                                                                                 |
| never (5)        |                                                                                    | <p>永不存在的值的类型，那些总会抛出异常<br>或者根本不会有返回值的函数表达式或者箭头函数表达式的返回值类型 <br> 变量也可能是 nerver 类型，当他们被永不为真的类型保护所约束时</p> |
| Object           |                                                                                    | 表示非原始类型，除了 number, string, boolean, symbol, null 或者 undefined之外的类型                                   |
| 类型断言 (6)         |                                                                                    |                                                                                                      |

(1): 其中 `Array` 即为接受参数的泛型接口,如下:

```javascript
interface Array<T> {
    find<S extends T>(predicate: (this: void, value: T, index: number, obj: T[]) => value is S, thisArg?: any): S | undefined;
    ...
    find(predicate: (value: T, index: number, obj: T[]) => unknown, thisArg?: any): T | undefined;
    ...
    findIndex(predicate: (value: T, index: number, obj: T[]) => unknown, thisArg?: any): number;
    ...
    fill(value: T, start?: number, end?: number): this;
    ...
    copyWithin(target: number, start: number, end?: number): this;
}
```

(2): 枚举

编译之后的代码需要注意:

```
enum Color { Red, Green, Blue }
let c: Color = Color.Green;  // 1  

编译之后为:
var Color;
(function (Color) {
    Color[Color["Red"] = 0] = "Red";
    Color[Color["Green"] = 1] = "Green";
    Color[Color["Blue"] = 2] = "Blue";
})(Color || (Color = {}));
let c = Color.Green;

此时Color为:  
{
    0: Red,
    1: Green,
    2: Blue,
    Red: 0,
    Green: 1,
    Blur: 2
}

因此 Color.Green = 1;
```

默认情况下，从0开始为元素编号，但是可以手动指定成员的数值:

```
enum Color { Red = 1,Green = 2,Blue = 4}

此时 Color 为: 
{
    1: Red,
    2: Green,
    4: Blue,
    Red: 1,
    Green: 2,
    Blue: 4
}
```

枚举的便利在于可以由枚举的值得到它的名字:

```javascript
enum Color { Red=1,Green,Blue };

let colorName: string = Color[2];    // 'Green';
```

(3): Any 与 Object 的区别

Object类型的变量允许你给它赋任意值，但是却不能调用它上面任意的方法，即使它真的有这些方法:

```
let notSure: any = 4;
notSure.ifExist();

let prettySure: Object = 4.1;
prettySure.toFixed(); // error
prettySure.toString(); // true
```

(4): void

当函数没有返回值时，

```
function warnUser(): void {
    console.log('warn')
}
```

void类型没有什么大用，只能赋予 undefined 或者 null

```
let unusable: void = undefined;
let unusable: void = null;
```

(5): never

```
// 返回never的函数必须存在无法达到的终点
function error(message: string): never {
    throw new Error(message)
}

// 推断的返回值类型为never
function fail() {
    return error('somgthing failed')
}

// 返回never的函数必须存在无法达到的终点
function infiniteLoop(): never {
    while (true) {

    }
}
```

(6): 类型断言

会遇到这样的情况，你比 typescript 更了解某个值的详细信息，通常发生在你清楚的知道一个实体具有比它现有类型更确切的类型。

通过类型断言告诉编译器，"相信我，我知道自己在干什么"

类型断言的两种形式:

尖括号形式

```
let str: any = "123";
let strLength: number = (<string>str).length;
```

as 语法

```
let str: any = "123";
let strLength: number = (str as string).length
```


---

# 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://yes-1-am.gitbook.io/blog/typescript-yu-fa/ji-chu-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.
