ts-帮助
各种打字助手函数和工具类型
特性
价值跟踪
此库包含许多助手类型和功能, 如 add, (中文(简体) ), subtract,以及 substring 允许值
在编译时已知

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
缺点
- 这些修饰器类型目前只能在使用站点使用,这意味着您必须记住在您类型的所有用法上使用它们. 见这些问题:
- 可能不太适合处理更复杂的类型 如果你遇到这样的事, 提出问题
- 私人方法不起作用
UnsafeVariance
不用说这些修饰者 别 更改函数的运行时间行为。 箭头函数仍然是箭头函数,无论是否使用 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'(中文(简体) )