Created
October 12, 2024 16:46
-
-
Save m-fire/4c6d8d9400451483ee12e5b888023d1a to your computer and use it in GitHub Desktop.
microsoft/tyringe DI Kit `d.ts` extension: Abstraction class can be used as `InjectionToken<T>`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <reference types="tsyringe" /> | |
import constructor from 'tsyringe/dist/typings/types/constructor' | |
import InjectionToken from 'tsyringe/dist/typings/providers/injection-token' | |
import InterceptionOptions from 'tsyringe/dist/typings/types/interceptor-options' | |
import { | |
PostResolutionInterceptorCallback, | |
PreResolutionInterceptorCallback, | |
} from 'tsyringe/dist/typings/types/dependency-container' | |
import ValueProvider from 'tsyringe/dist/typings/providers/value-provider' | |
import FactoryProvider from 'tsyringe/dist/typings/providers/factory-provider' | |
import TokenProvider from 'tsyringe/dist/typings/providers/token-provider' | |
import ClassProvider from 'tsyringe/dist/typings/providers/class-provider' | |
import Transform from 'tsyringe/dist/typings/types/transform' | |
/** | |
* @description | |
* - Existing(^4.8.0): The InjectionToken type cannot use an abstract class as a token. | |
* - Enhanced: By adding an instance inference type InstanceType<T> of the abstract class | |
* to InjectionToken, the abstract class can be used as a token. | |
*/ | |
declare module 'tsyringe' { | |
export type ExtendedInjectionToken<T = any> = InjectionToken<T> & | |
InstanceType<T> | |
export interface RegistryBase<T> { | |
entries(): IterableIterator<[ExtendedInjectionToken<T>, T[]]> | |
getAll(key: ExtendedInjectionToken<T>): T[] | |
get(key: ExtendedInjectionToken<T>): T | null | |
set(key: ExtendedInjectionToken<T>, value: T): void | |
setAll(key: ExtendedInjectionToken<T>, value: T[]): void | |
has(key: ExtendedInjectionToken<T>): boolean | |
} | |
export function defineInstanceTypeMetadata( | |
data: any, | |
transform?: { | |
transformToken: ExtendedInjectionToken<Transform<any, any>> | |
args: any[] | |
}, | |
): ( | |
target: any, | |
propertyKey: string | symbol | undefined, | |
parameterIndex: number, | |
) => any | |
export interface TokenDescriptor { | |
token: ExtendedInjectionToken<any> | |
multiple: boolean | |
} | |
export interface TransformDescriptor { | |
token: ExtendedInjectionToken<any> | |
transform: ExtendedInjectionToken<Transform<any, any>> | |
transformArgs: any[] | |
} | |
export function isNormalToken( | |
token?: ExtendedInjectionToken<any>, | |
): token is string | symbol | |
export function isConstructorToken( | |
token?: ExtendedInjectionToken<any>, | |
): token is constructor<any> | DelayedConstructor<any> | |
export default function scoped<T>( | |
lifecycle: Lifecycle.ContainerScoped | Lifecycle.ResolutionScoped, | |
token?: ExtendedInjectionToken<T>, | |
): (target: constructor<T>) => void | |
export function registry( | |
registrations?: ({ | |
token: ExtendedInjectionToken | |
options?: RegistrationOptions | |
} & Provider<any>)[], | |
): (target: any) => any | |
export function injectWithTransform( | |
token: ExtendedInjectionToken<any>, | |
transformer: ExtendedInjectionToken<Transform<any, any>>, | |
...args: any[] | |
): ( | |
target: any, | |
propertyKey: string | symbol | undefined, | |
parameterIndex: number, | |
) => any | |
export function injectAll( | |
token: ExtendedInjectionToken<any>, | |
): ( | |
target: any, | |
propertyKey: string | symbol | undefined, | |
parameterIndex: number, | |
) => any | |
export function injectAllWithTransform( | |
token: ExtendedInjectionToken<any>, | |
transformer: ExtendedInjectionToken<Transform<[any], any>>, | |
...args: any[] | |
): ( | |
target: any, | |
propertyKey: string | symbol | undefined, | |
parameterIndex: number, | |
) => any | |
export function inject( | |
token: ExtendedInjectionToken<any>, | |
): ( | |
target: any, | |
propertyKey: string | symbol | undefined, | |
parameterIndex: number, | |
) => any | |
export interface DependencyContainer extends Disposable { | |
register<T>( | |
token: ExtendedInjectionToken<T>, | |
provider: ValueProvider<T>, | |
): DependencyContainer | |
register<T>( | |
token: ExtendedInjectionToken<T>, | |
provider: FactoryProvider<T>, | |
): DependencyContainer | |
register<T>( | |
token: ExtendedInjectionToken<T>, | |
provider: TokenProvider<T>, | |
options?: RegistrationOptions, | |
): DependencyContainer | |
register<T>( | |
token: ExtendedInjectionToken<T>, | |
provider: ClassProvider<T>, | |
options?: RegistrationOptions, | |
): DependencyContainer | |
register<T>( | |
token: ExtendedInjectionToken<T>, | |
provider: constructor<T>, | |
options?: RegistrationOptions, | |
): DependencyContainer | |
registerSingleton<T>( | |
from: ExtendedInjectionToken<T>, | |
to: ExtendedInjectionToken<T>, | |
): DependencyContainer | |
registerType<T>( | |
from: ExtendedInjectionToken<T>, | |
to: ExtendedInjectionToken<T>, | |
): DependencyContainer | |
registerInstance<T>( | |
token: ExtendedInjectionToken<T>, | |
instance: T, | |
): DependencyContainer | |
resolve<T>(token: ExtendedInjectionToken<T>): T | |
resolveAll<T>(token: ExtendedInjectionToken<T>): T[] | |
isRegistered<T>( | |
token: ExtendedInjectionToken<T>, | |
recursive?: boolean, | |
): boolean | |
beforeResolution<T>( | |
token: ExtendedInjectionToken<T>, | |
callback: PreResolutionInterceptorCallback<T>, | |
options?: InterceptionOptions, | |
): void | |
afterResolution<T>( | |
token: ExtendedInjectionToken<T>, | |
callback: PostResolutionInterceptorCallback<T>, | |
options?: InterceptionOptions, | |
): void | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment