Skip to content

性能优化

用于优化 TypeScript 编译和类型检查的类型。

性能类型 (v1.11.0)

新增的性能优化类型,用于高级场景。

Fast & Optimized

typescript
import type { Fast, Optimized, CachedCompute, LazyCompute } from 'uni-types'

// Fast 类型 - 优化编译速度
type FastType = Fast<{ a: string } & { b: number }>
// { a: string; b: number }

// Optimized 类型 - 平衡优化
type OptType = Optimized<ComplexType>

// 缓存计算
type Cached = CachedCompute<string>

// 懒计算
type LazyComp = LazyCompute<string>  // () => string

编译优化

typescript
import type {
  ReduceComplexity,
  SimplifyForCompiler,
  OptimizeInference,
  ReduceRecursion,
  RecursionLimit
} from 'uni-types'

// 降低类型复杂度
type Reduced = ReduceComplexity<NestedType>

// 为编译器简化
type Simple = SimplifyForCompiler<{ a: string } & { b: number }>

// 优化推断
type Inferred = OptimizeInference<T>

// 限制递归深度
type Limited = RecursionLimit<DeepType, 10>

内存优化

typescript
import type {
  LightWeight,
  Minimal,
  SharedStructure,
  Pooled,
  InternedString
} from 'uni-types'

// 轻量表示
type Light = LightWeight<HeavyType>

// 最小表示(排除 undefined)
type Min = Minimal<{ a: string; b?: undefined }>

// 共享结构
type Shared = SharedStructure<{ a: string }>

// 池化类型
type PooledType = Pooled<number>

// 内部字符串
type Interned = InternedString

构建性能

typescript
import type {
  Precompute,
  PrecomputedValue,
  DeferredEvaluation,
  BuildHint,
  SkipCheck
} from 'uni-types'

// 预计算类型
type Pre = Precompute<ComplexType>

// 预计算值
type PreValue = PrecomputedValue<string, 'literal'>

// 延迟求值
type Deferred = DeferredEvaluation<string>

// 构建提示
type Hinted = BuildHint<string>

性能监控

typescript
import type {
  TypeComplexityMetrics,
  CompilationTime,
  TypeSize,
  PerformanceHint,
  PerformanceOptimization
} from 'uni-types'

// 类型复杂度指标
interface Metrics extends TypeComplexityMetrics<MyType> {}

// 编译时间估算
interface Time extends CompilationTime<MyType> {}

// 类型大小估算
interface Size extends TypeSize<MyType> {}

// 性能提示
type Hinted = PerformanceHint<string>

优化策略

typescript
import type {
  OptimizationStrategy,
  OptimizationLevel,
  OptimizationConfig,
  OptimizationResult
} from 'uni-types'

// 策略: 'aggressive' | 'balanced' | 'conservative' | 'debug'
type Strategy = OptimizationStrategy

// 级别: 0 | 1 | 2 | 3
type Level = OptimizationLevel

// 配置
interface Config extends OptimizationConfig {}

懒加载类型求值

延迟类型求值以获得更好的编译性能。

typescript
import type { Lazy, ForceEvaluate, Deferred, Thunk } from 'uni-types'

// 懒加载类型包装
type LazyString = Lazy<string>  // () => string

// 强制求值
type Evaluated = ForceEvaluate<LazyString>  // string

// 延迟类型(防止立即展开)
type Complex = Deferred<{ a: string } & { b: number }>

// Thunk 类型
type StringThunk = Thunk<string>  // () => string

类型缓存

缓存类型计算以防止重复求值。

typescript
import type { Cached, CachedValue, Memoized } from 'uni-types'

// 缓存类型
type CachedString = Cached<string>  // { __cached: string }

// 提取缓存值
type Value = CachedValue<CachedString>  // string

// 记忆化类型计算
type Memo = Memoized<{ a: string } & { b: number }>

类型优化

简化和优化复杂类型。

Simplify

展平交叉类型。

typescript
import type { Simplify } from 'uni-types'

type Complex = { a: string } & { b: number }
type Simple = Simplify<Complex>
// { a: string; b: number }

DeepSimplify

递归简化嵌套类型。

typescript
import type { DeepSimplify } from 'uni-types'

type Nested = { 
  a: { b: string } & { c: number } 
}
type Deep = DeepSimplify<Nested>
// { a: { b: string; c: number } }

Compact

移除 neverundefined 属性。

typescript
import type { Compact } from 'uni-types'

type Compacted = Compact<{ a: string; b: never; c?: undefined }>
// { a: string }

StripNever / StripUndefined / StripNull

从对象属性中移除特定类型。

typescript
import type { StripNever, StripUndefined, StripNull } from 'uni-types'

type A = StripNever<{ a: string; b: never; c: number }>
// { a: string; c: number }

type B = StripUndefined<{ a: string; b?: undefined }>
// { a: string }

MergeAll

合并多个对象类型。

typescript
import type { MergeAll } from 'uni-types'

type Merged = MergeAll<[{ a: string }, { b: number }, { c: boolean }]>
// { a: string; b: number; c: boolean }

TypeEq

在编译时检查类型相等性。

typescript
import type { TypeEq } from 'uni-types'

type A = TypeEq<string, string>   // true
type B = TypeEq<string, number>   // false

基于 MIT 许可发布