@viso/shared-types
v0.2.3
Published
一个全面的 TypeScript 类型工具库,提供各种实用的类型定义和类型工具,专为复杂的 TypeScript 项目设计。
Readme
@viso/shared-types
一个全面的 TypeScript 类型工具库,提供各种实用的类型定义和类型工具,专为复杂的 TypeScript 项目设计。
安装
npm install @viso/shared-types功能特点
- 🎯 类型安全: 完全基于 TypeScript 实现,提供丰富的类型定义
- 🔧 工具类型: 包含各种实用的类型工具和辅助类型
- 🧬 类型保护: 提供类型守卫函数,确保运行时类型安全
- 🏷️ 品牌类型: 支持品牌类型,增强类型区分度
- 📊 数据结构: 提供各种数据结构的类型定义
- 🔍 深度对象: 支持深度对象类型操作
- 📚 数组工具: 丰富的数组类型工具
- 🎨 条件类型: 强大的条件类型支持
使用方法
对象键类型
import { ObjectKeys, StrictObjectKeys } from '@viso/shared-types'
interface User {
id: number
name: string
email: string
}
// 获取对象的键类型
type UserKeys = ObjectKeys<User> // 'id' | 'name' | 'email'函数类型工具
import { FunctionType, AsyncFunction } from '@viso/shared-types'
// 函数类型定义
type MyFunction = FunctionType<[string, number], boolean>
// 异步函数类型
type MyAsyncFunction = AsyncFunction<[string], User>类型守卫
import { isString, isNumber, isObject, isArray } from '@viso/shared-types'
function processValue(value: unknown) {
if (isString(value)) {
// value 现在被推断为 string
console.log(value.toUpperCase())
}
if (isNumber(value)) {
// value 现在被推断为 number
console.log(value.toFixed(2))
}
}品牌类型
import { Brand } from '@viso/shared-types'
// 创建品牌类型
type UserId = Brand<number, 'UserId'>
type ProductId = Brand<number, 'ProductId'>
function getUser(id: UserId) {
// 只接受 UserId 类型,不接受普通 number
}
const userId = 123 as UserId
getUser(userId) // ✅ 正确
// getUser(123) // ❌ 类型错误数据结构类型
import { Maybe, Result, Either } from '@viso/shared-types'
// Maybe 类型(可选值)
type MaybeUser = Maybe<User>
// Result 类型(成功或失败)
type UserResult = Result<User, Error>
// Either 类型(左或右)
type UserOrError = Either<Error, User>数组类型工具
import { Head, Tail, Last, NonEmptyArray } from '@viso/shared-types'
type Numbers = [1, 2, 3, 4, 5]
type First = Head<Numbers> // 1
type Rest = Tail<Numbers> // [2, 3, 4, 5]
type LastItem = Last<Numbers> // 5
// 非空数组类型
type NonEmptyNumbers = NonEmptyArray<number>条件类型
import { If, Extends, Not } from '@viso/shared-types'
// 条件类型
type IsString<T> = If<Extends<T, string>, true, false>
type Test1 = IsString<string> // true
type Test2 = IsString<number> // false深度对象操作
import { DeepPartial, DeepRequired, DeepReadonly } from '@viso/shared-types'
interface NestedUser {
profile: {
personal: {
name: string
age: number
}
social: {
email: string
phone?: string
}
}
}
// 深度可选
type PartialNestedUser = DeepPartial<NestedUser>
// 深度必需
type RequiredNestedUser = DeepRequired<NestedUser>
// 深度只读
type ReadonlyNestedUser = DeepReadonly<NestedUser>模块说明
核心模块
- object-keys: 对象键类型工具
- functions: 函数类型定义和工具
- data-structures: 常用数据结构类型
- type-guards: 类型守卫函数
- branded-types: 品牌类型支持
- arrays: 数组类型工具
- conditional: 条件类型工具
- literal-types: 字面量类型工具
- deep-object: 深度对象操作类型
- advanced: 高级类型工具
API 参考
类型守卫
isString(value): 检查是否为字符串isNumber(value): 检查是否为数字isBoolean(value): 检查是否为布尔值isObject(value): 检查是否为对象isArray(value): 检查是否为数组isFunction(value): 检查是否为函数isNull(value): 检查是否为 nullisUndefined(value): 检查是否为 undefined
工具类型
Brand<T, B>: 品牌类型Maybe<T>: 可选值类型Result<T, E>: 结果类型Either<L, R>: Either 类型NonEmptyArray<T>: 非空数组类型DeepPartial<T>: 深度可选类型DeepRequired<T>: 深度必需类型DeepReadonly<T>: 深度只读类型