@detachhead/ts-helpers

中文文档
16.2.0发布于 2023/09/19ISCTypeScriptCommonJS近 30 天下载 3242

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

中文使用文档
文档目录10 节
  1. 特性
  2. 价值跟踪
  3. noUncheckedIndexedAccess 支助
  4. 日期类型
  5. 类型测试
  6. 修改类型
  7. 铸造函数
  8. 所需经费
  9. 类型标记
  10. 运行时间

ts-帮助

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

npm

特性

价值跟踪

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

asdf

noUncheckedIndexedAccess 支助

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

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

可以通过 lengthGreaterThan

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 工具类型

日期类型

此库包含用于格式化日期的助手类型和函数 页:1 date-fns 格式.

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 函数可以测试类型或值是否与类型完全匹配

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 类型允许您检查两种类型在类型级别是否相等

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

修改类型

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

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

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

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 从子类 :

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

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

SafeVariance 编号 : ToArrowFunction

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

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']> 转换函数类型 :

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

缺点

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

铸造函数

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 共发布 233 个版本,首次发布于 2021/01/12;过去 12 个月发布了 0 个版本。

版本发布日期距今
16.2.0latest2023/09/193 年前
16.1.0-8075f1b69fcb8e43f9190d228d110fd30d45f9fccanary2023/09/193 年前
16.1.0-4d936d7f443b76961f9cc384ab5fa4cabe73bf9d2023/07/243 年前
16.1.0-2467287e1c6ba90ad367c35fbdea72696b04d8272023/07/243 年前
16.1.0-f64e5159dba50b1b14d5e79bcbd36ef96ff3ce672023/07/243 年前
16.1.0-064bd6e36fa1652dd72ff70f5ea2228eb7d089242023/07/243 年前

关于 @detachhead/ts-helpers 的常见问题

@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 和提交记录判断。