All files / src/utils get-injectable-token.mts

100% Statements 4/4
100% Branches 2/2
100% Functions 1/1
100% Lines 4/4

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                      1711x 1711x 3x     1708x    
import type { ClassType, InjectionToken } from '../token/injection-token.mjs'
 
import { DIError } from '../errors/di-error.mjs'
import { InjectableTokenMeta } from '../symbols/index.mjs'
 
export function getInjectableToken<R>(
  target: ClassType,
): R extends { create(...args: any[]): infer V }
  ? InjectionToken<V>
  : InjectionToken<R> {
  // @ts-expect-error We inject the token into the class itself
  const token = target[InjectableTokenMeta] as InjectionToken<any, any>
  if (!token) {
    throw DIError.classNotInjectable(target.name)
  }
  // @ts-expect-error We detect factory or class
  return token
}