Interface TypeKey<T, Def>

A key for a custom dependency not bound to a specific class.

See

The TypeKey() function for implementing TypeKey. A TypeKey implementation is a class object that extends TypeKey().

Example

// These TypeKeys both resolve to `string` but are separate from each other:
class NameKey extends TypeKey<string>() { private _: any }
class IdKey extends TypeKey<string>() { private _: any }

class User { constructor(name: string, id: string) {} }

const ct = Container.create()
.provideInstance(NameKey, 'Alice')
.provideInstance(IdKey, '123')
.provide(User, {
name: NameKey,
id: IdKey,
}, ({ name, id }) => new User(name, id))

const user = ct.request(User)

Type Parameters

Hierarchy

  • BaseTypeKey<T, Def>
  • AbstractKey
    • TypeKey

Constructors

Properties

DependencyKey Operators

Constructors

constructor

Properties

fullName: string

The name that will be displayed in exception messages.

of?: ClassLike<T>

A class or function returning the target value of this TypeKey.

scope?: ScopeList | (() => ScopeList)

The Scope or ScopeList to which this TypeKey should be bound.

Type declaration

DependencyKey Operators

  • Resolves to a Promise of the target value, allowing a synchronous resource to depend on an asynchronous one.

    Type Parameters

    Parameters

    • this: Th

    Returns Async<Th>

    See

    async

  • Applicable when this is a key that resolves to a function, for example a SubcomponentDefinition or a FactoryKey. Resolves to the output of the function when called with args.

    class UserFactory extends FactoryKey(
    (name: string, id: number) => new User(name, id),
    ) { private _: any }

    container.inject({ user: UserFactory.Build(name, id) }, ({ user }) => {
    console.log(user.name, user.id)
    })

    Type Parameters

    • Th extends SimpleKey<((...args) => Out)>

    • Args extends any[]

    • Out = Th extends SimpleKey<((...args) => O)>
          ? O
          : never

    Parameters

    • this: Th
    • Rest ...args: Args

    Returns Build<Th, Args, Out>

    See

    build

  • Applies the given transform to the resolved value of this DependencyKey.

    Type Parameters

    Parameters

    • this: Th
    • transform: ((x) => U)
        • (x): U
        • Parameters

          Returns U

    Returns Map<U, Th, P>

    Example

    class IdNum extends TypeKey<number>() { private _: any }
    class IdStr extends TypeKey({
    default: IdNum.Map(id => id.toString()),
    }) { private _: any }

    See

    map

  • Requests a value for this key, without compile-time verification of dependencies. An exception will be thrown if the dependency is not available upon request.

    Static verification that this key can be resolved synchronously is still performed.

    Type Parameters

    Parameters

    • this: Th

    Returns Unchecked<Th>

Generated using TypeDoc