All files / src/utils types.mts

0% Statements 0/0
0% Branches 0/0
0% Functions 0/0
0% Lines 0/0

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58                                                                                                                   
import type {
  BoundInjectionToken,
  ClassType,
  FactoryInjectionToken,
  InjectionToken,
} from '../token/injection-token.mjs'
 
// Utility types for string manipulation and union handling
export type Join<TElements, TSeparator extends string> =
  TElements extends Readonly<[infer First, ...infer Rest]>
    ? Rest extends ReadonlyArray<string>
      ? First extends string
        ? `${First}${Rest extends [] ? '' : TSeparator}${Join<Rest, TSeparator>}`
        : never
      : never
    : ''
 
// credits goes to https://stackoverflow.com/a/50375286
export type UnionToIntersection<U> = (
  U extends any ? (k: U) => void : never
) extends (k: infer I) => void
  ? I
  : never
 
// Converts union to overloaded function
export type UnionToOvlds<U> = UnionToIntersection<
  U extends any ? (f: U) => void : never
>
 
export type PopUnion<U> =
  UnionToOvlds<U> extends (a: infer A) => void ? A : never
 
export type IsUnion<T> = [T] extends [UnionToIntersection<T>] ? false : true
 
export type UnionToArray<T, A extends unknown[] = []> =
  IsUnion<T> extends true
    ? UnionToArray<Exclude<T, PopUnion<T>>, [PopUnion<T>, ...A]>
    : [T, ...A]
 
export type InjectRequest = {
  token:
    | InjectionToken<any>
    | BoundInjectionToken<any, any>
    | FactoryInjectionToken<any, any>
    | ClassType
  promise: Promise<any>
  readonly result: any
  readonly error: Error | null
}
 
// InjectState interface for managing injection state
export interface InjectState {
  currentIndex: number
  isFrozen: boolean
  requests: InjectRequest[]
}