@ brixtol/ tsutils
文本工具类型 Brixtol 纺织品公司 单曲工作空间。 这个模块基于松散的 类型庆典 意思是提供了一些必要的类型.
安装
pnpm add @brixtol/tsutils
使用量
通过命名空间导入模块 。 下面的例子显示了一些公用事业。 附加上下文请查看 测试 目录.
import { AnyCase, ArrayMin, ConcatUnion } from '@brixtol/tsutils';
let string: AnyCase<'foo'>; // 'FOO' | 'FoO' | 'fOO' etc etc
let concat: ConcatUnion<'A' | 'B', '-'>; // 'A' | 'B' | 'A-B' etc etc
let arrmin: ArrayMin<2, string>; // ['a', 'b']
// etc etc
公用事业
该套件提供了多套有用的TypeScript工具. 如果你需要更多,请看 类型庆典下面是所包括的内容以及如何使用这些内容的一些基本例子.
AnyCase<string>
将字符串结合转换为 AnyCase 组合,
const anycase: AnyCase<'foo'> = (
| 'FOO'
| 'fOO'
| 'foO'
| 'Foo'
| 'FOo'
| 'foo'
| 'fOo'
| 'FoO'
);
ArrayFixed<number, type>
确保一个阵列有固定的长度,因为它不能比所提供的长度多或少.
// ✓ VALID
const a: ArrayFixed<2, string> = ['A', 'B'];
const b: ArrayFixed<3, string> = ['A', 'B', 'C'];
// 𐄂 INVALID
const c: ArrayFixed<2, string> = ['A'];
ArrayMax<number, type>
确保数组的长度最大 。 如果数组的大小超过长度,则会抛出错误.
// ✓ VALID
const a: ArrayMax<3, string> = ['A', 'B'];
const b: ArrayMax<4, string> = ['A', 'B', 'C'];
// 𐄂 INVALID
const c: ArrayMax<2, string> = ['A', 'B', 'C'];
ArrayMin<number, type>
确保数组类型具有最小长度。 如果数组的大小小于长度,则会抛出错误.
// ✓ VALID
const a: ArrayMin<2, string> = ['A', 'B'];
const b: ArrayMin<3, string> = ['A', 'B', 'C'];
// 𐄂 INVALID
const c: ArrayMin<2, string> = ['A'];
ArrayRange<number, number, type>
确保数组长度大于 from 大小和小于 to 尺寸.
// ✓ VALID
const a: ArrayRange<2, 4, string> = ['A', 'B'];
const b: ArrayRange<1, 3, string> = ['A', 'B', 'C'];
// 𐄂 INVALID
const c: ArrayRange<2, 4, string> = ['A'];
ConcatUnion<union, separator>
组合字符串联盟,包括来自联盟类型和自定义分隔符的值.
const union: ConcatUnion<'A' | 'B' | 'C', '-'> = (
| 'A'
| 'B'
| 'C'
| 'A-B'
| 'B-A'
| 'A-C'
| 'B-C'
| 'A-B-C'
| 'B-A-C'
| 'C-A'
| 'C-B'
| 'C-A-B'
| 'C-B-A'
);
Join<string, separator>
使用给定的字符串作为分隔符加入一系列字符串和/或数字。 用于定义嵌入对象中的密钥路径 .
被从 类型庆典
// Mixed (strings & numbers) items; result is: 'foo.0.baz'
const path: Join<['foo', 0, 'baz'], '.'> = 'foo.0.baz';
*
// Only string items; result is: 'foo.bar.baz'
const path: Join<['foo', 'bar', 'baz'], '.'> = 'foo.bar.baz'
*
// Only number items; result is: '1.2.3'
const path: Join<[1, 2, 3], '.'> = [1, 2, 3].join('.');
JoinProps<object, separator?>
深层引用对象键生成字符串字形平整结合,带有选项分隔符字符,默认为 . 点.
const props: JoinProps<{
a: string;
b: {
c: string;
d: {
e: {
f: string;
g: string;
}[];
}
}
> = (
| "a"
| "b"
| "b.c"
| "b.d.e"
| "b.d"
| `b.d.e.${number}.f`
| `b.d.e.${number}.g`
| `b.d.e.${number}`
)
LiteralUnion<union>
允许通过字符串类型,同时尊重intellisense补全.
const literal: LiteralUnion<'A' | 'B'> = (
| 'A'
| 'C'
| 'D' // D is allowed to pass
)
Merge<object, object>
将两种类型合并为新类型。 第二类密钥会覆盖第一类密钥.
被从 类型庆典
type Foo = { a: number; b: string };
type Bar = { b: number };
const ab: Merge<Foo, Bar> = { a: 1, b: 2 };
NumberRange<number, number>
生成2个数字之间的数字结合,不包括 TO 数字限制,只返回相距范围.
const range: Range<1, 5> = (
| 1
| 2
| 3
| 4
)
ObjectEntries<object>
将对象转换为数组条目列表。 其类型相当于 Object.entries() 方法.
const entries: ObjectEntries<{
foo: { a: 1 },
bar: { a: 2 }
}> = (
[
['foo', { a: 1 }],
['bar', { a: 2 }],
]
)
OmitProps<string[], object | object[]>
用于从字符串列表中排除对象属性,并将返回对象所保留属性. 当通过一个 Array 界面将返回值为数组,否则返回值为对象.
// OBJECT RETURN TYPE
const array: OmitProps<[
'foo',
'baz'
], {
foo: { a: string },
bar: { a: string },
baz: { a: string }
}> = {
bar: { a: 'one' },
}
// ARRAY RETURN TYPE
const array: OmitProps<[
'foo',
'baz'
], Array<{
foo: { a: string },
bar: { a: string },
baz: { a: string }
}>
> = [
{
bar: { a: 'one' },
}
]
PartialDeep<object>
用于从另一个类型创建类型,所有密钥和嵌入密钥设置为可选.
被从 类型庆典
interface IObject {
deep: {
a: string;
b: string;
c: {
d: any;
e: number;
f: {
g: string[]
}
}
}
}
// RESULT
const object: PartialDeep<IObject> = {
deep: {
a: 'foo'
}
};
PickProps<string[], object | object[]>
用于从字符串列表中选择对象属性,并将返回对象上选定的属性。 当通过一个 Array 界面将返回值为数组,否则返回值为对象.
// OBJECT RETURN TYPE
const array: PickProps<[
'foo',
'baz'
], {
foo: { a: string },
bar: { a: string },
baz: { a: string }
}> = {
foo: { a: 'one' },
baz: { a: 'one' },
}
// ARRAY RETURN TYPE
const array: OmitProps<[
'foo',
'baz'
], Array<{
foo: { a: string },
bar: { a: string },
baz: { a: string }
}>
> = [
{
foo: { a: 'one' },
baz: { a: 'one' },
}
]
Split<string, separator>
代表使用给定字符集或字符集分割字符串的数组。 通常用于返回类型方法,如 String.prototype.split。 。 。 .
被从 类型庆典
const split: Split<'a,b,c,d', ','> = ['a','b','c','d']
Valueof<object>
对面 keyof 返回对象值为联盟类型.
const values: Valueof<{
foo: 'a',
bar: 'b',
baz: 'c'
}> = (
| 'a'
| 'b'
| 'c'
)
许可证
许可证根据 麻省理工学院
我们 ♡ 开源!