# @detachhead/ts-helpers 中文文档

规范地址：https://npmcn.com/package/%40detachhead/ts-helpers/

版本：16\.2\.0
npm 元数据更新时间：2023-09-19T09:02:40.418Z
中文译文更新时间：2026-11-12T02:36:03.585Z
软件包许可：ISC

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40detachhead/ts-helpers
原文对照：https://npmcn.com/package/%40detachhead/ts-helpers/?lang=original
项目主页：https://github.com/DetachHead/ts-helpers#readme
源码仓库：https://github.com/DetachHead/ts-helpers

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：338 kB，90 个文件
- 模块格式：CommonJS
- TypeScript 类型：包内自带
- Node.js 要求：\>=16\.6\.0
- 运行依赖：10 个
- 首次发布：2021-01-12T15:40:58.264Z
- 最新版本发布：2023-09-19T09:02:40.253Z
- 近 30 天下载量：3242（2026-10-12 至 2026-11-10，npm 官方下载统计）
- 已发布版本数：233（近 12 个月 0 个）

## 中文简介

各种打字助手函数和工具类型

## 安装

```sh
npm install @detachhead/ts-helpers
```

也可以使用：`pnpm add @detachhead/ts-helpers`、`yarn add @detachhead/ts-helpers`、`bun add @detachhead/ts-helpers`

## README

# ts-帮助

各种打字助手函数和工具类型

[![npm](https://img.shields.io/npm/v/@detachhead/ts-helpers)](https://npmjs.org/@detachhead/ts-helpers)

## 特性

### 价值跟踪

此库包含许多助手类型和功能, 如 `add`, (中文(简体) ), `subtract`,以及 `substring` 允许值 在编译时已知

![asdf](https://npmcn.com/package/%40detachhead/ts-helpers/readme%20pics/functions.gif)

### `noUncheckedIndexedAccess` 支助

页:1 `noUncheckedIndexedAccess` 编译器旗是史诗式的,但还有改进的余地. 例如,以下各点: 问题:

```ts
if (foo.length > 2) {
    const bar: string = foo[1] //error: string | undefined not assignable to string
}
```

可以通过 `lengthGreaterThan`

```ts
import { lengthGreaterThan } from '@detachhead/ts-helpers/dist/functions/Number'

if (lengthGreaterThan(foo, 2)) {
    const bar: string = foo[1] //no error, foo is casted to [string, string]
}
```

还有 `lengthLessThan`, (中文(简体) ), `lengthGreaterOrEqual`等 (中文(简体) ).

此库中的大部分阵列函数都跟踪长度, 大多是感谢 改为 [这个 `TupleOf` 工具类型](https://github.com/microsoft/TypeScript/issues/26223#issuecomment-674514787)

### 日期类型

此库包含用于格式化日期的助手类型和函数 页:1 [`date-fns`](https://date-fns.org/v2.21.1/docs/format) 格式.

```ts
import { formatDate } from '@detachhead/ts-helpers/dist/functions/Date'

const date = formatDate(new Date(), 'dd-MM-yyyy')

assert(date === '01/01/2021') //compile error, wrong date format
```

您可以使用任何日期格式 `date-fns` 接受,并 `FormatDate` 工具类型将生成模板文字 键以匹配您想要的日期格式.

### 类型测试

与 `exactly` 函数可以测试类型或值是否与类型完全匹配

```ts
import { exactly } from '@detachhead/ts-helpers/dist/functions/misc'

const a: 1 | 2 = 1
//values (also does a runtime assertion on the values)
exactly(1 as number, a) // error as `1 | 2` is not an exact match of `number`
exactly(1 as number, a as number) // no error
exactly(1 as 1 | 2, a) // no error

// mixed
exactly<number>()(a) // error as `1 | 2` is not an exact match of `number`
exactly<number>()(a as number) // no error
exactly<1 | 2>()(a) // no error

// types
type Foo = 1 | 2
exactly<1, Foo>() // error as `1 | 2` is not an exact match of `1`
exactly<1 | 2, Foo>() // no error
```

这个 `Equals` 类型允许您检查两种类型在类型级别是否相等

```ts
import { Equals } from '@detachhead/ts-helpers/dist/types/misc'

type Foo = Equals<number, 1 | 2> // false
type Bar = Equals<any, 10> // false
type Baz = Equals<unknown, never> // false
```

### 修改类型

当使用旧方法语法时,打字符不检查指派时的差异 :

```ts
declare class A<T> {
    set(value: T): void
    get(): T
}

const a = new A<number>()

const b: A<unknown> = a

b.set('')
a.get() // typescript thinks this is a number but it's actually a string
```

关于差异如何起作用的更多信息,请参见 [此公关](https://github.com/microsoft/TypeScript/pull/18654). TL; DR基本上是比旧方法语法更严格地检查箭头函数(出于后向相容性的原因). 这意味着尽可能使用箭头函数.

```ts
declare class A<T> {
    set: (value: T) => void
    get: () => T
}

const a = new A<number>()

const b: A<unknown> = a // error: Type 'A<number>' is not assignable to type 'A<unknown>'
```

然而不幸的是,箭头功能并非总能被使用. 有时你需要使用方法代替,例如,如果你需要访问 `super` 从子类 :

```ts
class B extends A<number> {
    get = () => super.get() // runtime error, arrow functions can't access super
}
```

变化的修饰符就在这里出现

#### `SafeVariance` 编号 : `ToArrowFunction`

您可以使用 `SafeVariance<A>` 允许严格差异检查该类中的方法。 或者,如果你需要将单个函数类型转换为箭头函数类型,可以使用 `ToArrowFunction<A['get']>`

```ts
import { SafeVariance, ToArrowFunction } from '@detachhead/ts-helpers/dist/types/Function'

class B<T> extends A<T> {
    override get() {
        return super.get()
    }
}

const a = new B<number>()

const b: SafeVariance<A<unknown>> = a // error
```

#### `UnsafeVariance` 编号 : `ToNonArrowFunction`

差异只是一个问题 当你处理的类 有可变状态。 如果您的类型是不可改变的, 您可能想要禁用差异检查, 而无需将您闪亮的新箭头功能转换为 cringe 旧方法 .

要做到这一点,只需使用 `UnsafeVariance<A>` 在你的课,或 `ToNonArrowFunction<A['set']>` 转换函数类型 :

```ts
import { ToNonArrowFunction, UnsafeVariance } from '@detachhead/ts-helpers/dist/types/Function'

declare class A<T> {
    doSomethingElseThatTotallyDoesntChangeTheValue: (value: T) => void
    get: () => T
}

const a = new A<number>()

const b: UnsafeVariance<A<unknown>> = a // no error
```

#### 缺点

-   这些修饰器类型目前只能在使用站点使用,这意味着您必须记住在您类型的所有用法上使用它们. 见这些问题:
    -   [函数](https://github.com/DetachHead/ts-helpers/issues/162)
    -   [类](https://github.com/DetachHead/ts-helpers/issues/184)
-   可能不太适合处理更复杂的类型 如果你遇到这样的事, [提出问题](https://github.com/DetachHead/ts-helpers/issues/new/choose)
-   [私人方法不起作用 `UnsafeVariance`](https://github.com/DetachHead/ts-helpers/issues/160)

不用说这些修饰者 _别_ 更改函数的运行时间行为。 箭头函数仍然是箭头函数,无论是否使用 `ToNonArrowFunction` 输入它.

### 铸造函数

```ts
import { narrow, narrowCast, unsafeNarrow } from '@detachhead/ts-helpers/dist/functions/misc'
```

页:1 `as` 关键字并不总是按你的意愿来 页:1 `narrow`, (中文(简体) ), `unsafeNarrow` 财务报告和审定财务报表 `narrowCast` 函数可用于以不同方式设定变量

|  | 修改原始变量的类型 | 返回带有已设定类型的值 | 将原始类型与铸造类型相交 | 允许非重叠类型 |
| --- | --- | --- | --- | --- |
| `as` 关键词 |  | 对 |  |  |
| `narrow` 函数 | 对 |  | 对 |  |
| `unsafeNarrow` 函数 | 对 |  | 对 | 对 |
| `narrowCast` 函数 |  | 对 | 对 | 对 |

## 所需经费

### 类型标记

由于这个软件包能推动打字编译器的极限,我经常更新它以依赖未释放 用于修复错误的打字符的版本, 以及确保即将发行的版本不会打破我的任何疯狂类型 . 当前它取决于 qq 5.0 的打字符。 您可能会遇到 type 错误和/ 或 性能问题 用旧版本.

### 运行时间

只要它支持es2021 你就应该乖 测试于 :

-   节点15
-   浏览器 (chrome qq85)
-   dano(使用esm.sh - eg. `import { exactly } from 'http://esm.sh/@detachhead/ts-helpers/dist/utilityFunctions/misc'`(中文(简体) )

## 常见问题

### @detachhead/ts-helpers 是什么？

各种打字助手函数和工具类型

### 如何安装 @detachhead/ts-helpers？

在项目目录运行 npm install @detachhead/ts-helpers，或使用 pnpm add / yarn add / bun add。

### @detachhead/ts-helpers 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### @detachhead/ts-helpers 支持 ESM 还是 CommonJS？

以 CommonJS 形式发布，可以直接 require\(\)；在 ES Module 项目中也可以用 import 引入（Node\.js 会把 module\.exports 作为默认导出）。

### @detachhead/ts-helpers 需要什么版本的 Node\.js？

package\.json 的 engines 字段要求 Node\.js \>=16\.6\.0。版本不符时 npm 默认只给出警告，开启 engine-strict 后会拒绝安装。

### @detachhead/ts-helpers 可以商用吗？

@detachhead/ts-helpers 使用 ISC 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。以上为概括，不构成法律意见，请以许可原文为准。

### @detachhead/ts-helpers 的下载量有多少？

根据 npm 官方下载统计，@detachhead/ts-helpers 在 2026/10/12 – 2026/11/10 这 30 天内被下载 3,242 次，日均约 108 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @detachhead/ts-helpers 还在维护吗？

最新版本 16\.2\.0 发布于 2023/09/19（3 年前）；至今共发布 233 个版本，其中过去 12 个月发布了 0 个；首次发布于 2021/01/12。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

