Skip to content

Commit

Permalink
refactor: use overload signatures
Browse files Browse the repository at this point in the history
  • Loading branch information
exuanbo committed Oct 14, 2024
1 parent 3c4bae7 commit 6d6614f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
7 changes: 4 additions & 3 deletions src/container.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {type InjectionConfig, isConfigLike} from './config'
import {assert, ErrorMessage, expectNever} from './errors'
import type {Injections} from './injection'
import type {Injection, Injections} from './injection'
import {useInjectionContext, withInjectionContext} from './injection-context'
import {getMetadata} from './metadata'
import {type InjectionProvider, isClassProvider, isFactoryProvider, isProvider, isValueProvider} from './provider'
Expand Down Expand Up @@ -83,7 +83,7 @@ export class Container {

register<Instance extends object>(Class: Constructor<Instance>): void
register<Value>(provider: InjectionProvider<Value>): void
register<Value>(providable: InjectionProvider<Value> | Constructor<Value & object>): void {
register<Value>(providable: Constructor<Value & object> | InjectionProvider<Value>): void {
if (isConstructor(providable)) {
const Class = providable
const metadata = getMetadata(Class)
Expand All @@ -104,7 +104,8 @@ export class Container {
}
}

resolve<Values extends unknown[]>(...injections: Injections<Values>): Values[number] {
resolve<Values extends unknown[]>(...injections: Injections<Values>): Values[number]
resolve<Value>(...injections: Injection<Value>[]): Value {
for (const injection of injections) {
if (isConfigLike(injection)) {
if (isProvider(injection)) {
Expand Down
5 changes: 3 additions & 2 deletions src/decorators.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {inject} from './inject'
import type {Injections} from './injection'
import type {Injection, Injections} from './injection'
import {metadataRegistry} from './metadata'
import type {InjectionScope} from './scope'
import type {Constructor, InjectionToken} from './token'
Expand Down Expand Up @@ -33,7 +33,8 @@ export function Scoped<This extends object>(scope: InjectionScope): ClassDecorat
}
}

export function Inject<Values extends unknown[]>(...injections: Injections<Values>): ClassFieldDecorator<Values[number]> {
export function Inject<Values extends unknown[]>(...injections: Injections<Values>): ClassFieldDecorator<Values[number]>
export function Inject<Value>(...injections: Injection<Value>[]): ClassFieldDecorator<Value> {
return (_value, _context) =>
function (this, _initialValue) {
return inject.by(this, ...injections)
Expand Down
8 changes: 5 additions & 3 deletions src/inject.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import {assert, ErrorMessage} from './errors'
import type {Injections} from './injection'
import type {Injection, Injections} from './injection'
import {useInjectionContext} from './injection-context'

export function inject<Values extends unknown[]>(...injections: Injections<Values>): Values[number] {
export function inject<Values extends unknown[]>(...injections: Injections<Values>): Values[number]
export function inject<Value>(...injections: Injection<Value>[]): Value {
const context = useInjectionContext()
assert(context, ErrorMessage.InjectOutsideOfContext)
const container = context.container
return container.resolve(...injections)
}

export namespace inject {
export function by<Values extends unknown[]>(thisArg: any, ...injections: Injections<Values>): Values[number] {
export function by<Values extends unknown[]>(thisArg: any, ...injections: Injections<Values>): Values[number]
export function by<Value>(thisArg: any, ...injections: Injection<Value>[]): Value {
const context = useInjectionContext()
assert(context, ErrorMessage.InjectOutsideOfContext)
const resolution = context.resolution
Expand Down

0 comments on commit 6d6614f

Please sign in to comment.