> For the complete documentation index, see [llms.txt](https://seek.gitbook.io/acr/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://seek.gitbook.io/acr/typescript-shi-yong.md).

# Typescript

虽然 acr 内置了全 api 的 ts 支持，但在使用时，还是有部分情况需要留意。基本上，我们都使用了 Declaration Merging 来完成扩展。

## 自定义类型

当我们新建了一个类型，而 ts 不能准确识别时，可以增加类似声明。

```javascript
import Acr from 'acr';

declare module 'acr' {
    interface DateType extends Acr.Chain {
        min(value: Date, message?: string): this;
    }

    interface Acr {
        date(name: string): DateType;
        date(options?: Acr.ChainOptions): DateType;
    }
}
```

首先，我们定义了一个 date 类型，为 date 准备了一个验证方法 min，然后我们在 Acr 中声明 date 类型。由于 type 的参数可能含有多种结构，使用重载声明了两次。这样我们就能在验证中直接使用了。

## 扩展内置验证

很多时候我们可能更需要扩展内置类型的验证规则。这也非常简单：

```javascript
declare module 'acr' {
    interface StringType {
        nickname(message?: string): this;
    }
}
```
