diff --git a/src/main/ts/di/Annotations.ts b/src/main/ts/di/Annotations.ts --- a/src/main/ts/di/Annotations.ts +++ b/src/main/ts/di/Annotations.ts @@ -9,6 +9,7 @@ export interface Dependency extends Dependency { @@ -34,11 +35,6 @@ export class Builder(...args: P) { - // K = "bar" - // M = "setValue" - // S[K] = Bar - // T[M] = (value: string) => void - // P[m] = (value: V) => void return any }, M extends keyof (T | X)>( target: X, memberName: M, @@ -48,17 +44,26 @@ export class Builder { + getDescriptor(): TypeRegistration T, S> { throw new Error(); } } +export interface DependencyOptions { + optional?: boolean; + default?: T; +} + +export interface LazyDependencyOptions extends DependencyOptions { + lazy: true; +} + interface Declaration { define(): Builder; - dependency(name: K, opts: { lazy: true }): Lazy; - dependency(name: K, opts?: any): Dependency; + dependency(name: K, opts: LazyDependencyOptions): Lazy; + dependency(name: K, opts?: DependencyOptions): Dependency; config(): Config; } diff --git a/src/main/ts/di/Configuration.ts b/src/main/ts/di/Configuration.ts --- a/src/main/ts/di/Configuration.ts +++ b/src/main/ts/di/Configuration.ts @@ -32,24 +32,23 @@ export interface RegistrationScope extends RegistrationScope { +export interface ServiceRegistration extends RegistrationScope { activation?: ActivationType; - params?: P; + params?: any; inject?: object | object[]; cleanup?: ((instance: T) => void) | string; } -export interface TypeRegistration extends ServiceRegistration { - $type: string | (new (...params: P) => T); - +export interface TypeRegistration any, S extends object> extends ServiceRegistration, S> { + $type: string | C; } -export interface FactoryRegistration extends ServiceRegistration { - $factory: string | ((...params: P) => T); +export interface FactoryRegistration any, S extends object> extends ServiceRegistration, S> { + $factory: string | F; } export interface ValueRegistration { @@ -64,12 +63,16 @@ export interface DependencyRegistration< default?: ContainerResolve; } +export interface LazyDependencyRegistration = ContainerKeys> extends DependencyRegistration { + lazy: true; +} + export type Registration = T extends primitive ? T : ( T | { [k in keyof T]: Registration } | - TypeRegistration | - FactoryRegistration | + TypeRegistration T, S> | + FactoryRegistration<() => T, S> | ValueRegistration | DependencyRegistration ); @@ -86,11 +89,11 @@ const _activationTypes: { [k in Activati call: 5 }; -export function isTypeRegistration(x: any): x is TypeRegistration { +export function isTypeRegistration(x: any): x is TypeRegistration any, any> { return (!isPrimitive(x)) && ("$type" in x); } -export function isFactoryRegistration(x: any): x is FactoryRegistration { +export function isFactoryRegistration(x: any): x is FactoryRegistration<() => any, any> { return (!isPrimitive(x)) && ("$factory" in x); } @@ -311,7 +314,7 @@ export class Configuration) { + _makeServiceParams(data: ServiceRegistration) { const opts: any = { owner: this._container }; @@ -365,7 +368,7 @@ export class Configuration, name: string) { + async _visitTypeRegistration(data: TypeRegistration any, S>, name: string) { argumentNotNull(data.$type, "data.$type"); this._enter(name); @@ -386,7 +389,7 @@ export class Configuration, name: string) { + async _visitFactoryRegistration(data: FactoryRegistration<() => any, S>, name: string) { argumentOfType(data.$factory, Function, "data.$factory"); this._enter(name); diff --git a/src/test/ts/mock/Bar.ts b/src/test/ts/mock/Bar.ts --- a/src/test/ts/mock/Bar.ts +++ b/src/test/ts/mock/Bar.ts @@ -6,8 +6,9 @@ export const service = define(); @service.declare({ foo: dependency("foo"), nested: { - lazy: dependency("foo", {lazy: true}) - } + lazy: dependency("foo", { lazy: true }) + }, + host: "value" }) export class Bar { barName = "bar"; @@ -18,7 +19,8 @@ export class Bar { foo?: Foo; nested?: { lazy: () => Foo - } + }, + host: string }) { if (_opts && _opts.foo)