Function TypeKey

Hierarchy

  • Generates a base class for a class object that extends TypeKey<T>. Classes that extend the returned base class should have a private _: any property (or any other private member) to ensure the key has its own unique type.

    Type Parameters

    • T

    Returns TypeKeyClass<T, never>

    A base class that can be extended to produce a TypeKey<T> class object.

    Example

    class NameKey extends TypeKey<string>() { private _: any }
    

    Example

    with Scope:

    class NameKey extends TypeKey<string>() {
    private _: any
    static scope = Singleton
    }

    Example

    with default instance:

    class NameKey extends TypeKey({ default: Inject.value('Alice') }) {
    private _: any
    }

    Example

    with custom default:

    class NameKey extends TypeKey({
    default: Inject.map(DataSource, ds => ds.getName()),
    }) {
    private _: any
    }

    Example

    using the TypeKey:

    Provide

    const ct = Container.create().provide(NameKey, () => 'Alice')
    

    Request

    const name: string = ct.request(NameKey)
    

    Use as dependency

    const UserModule = Module(ct => ct
    .provide(User, { name: NameKey }, ({ name }) => new User(name))
    )

    Use key operators

    const UserModule = Module(ct => ct
    .provide(User, { name: NameKey.Lazy() }, ({ name }) => new User(name()))
    )
  • Type Parameters

    • T

    Parameters

    Returns TypeKeyClass<T, never>

  • Type Parameters

    • Def extends ComputedKey<T, any, any, any, any>

    • T = Def extends ComputedKey<_T, any, any, any, any>
          ? _T
          : never

    Parameters

    Returns TypeKeyClass<T, Def>

Generated using TypeDoc