Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(decorator): add decorator functionality (#6)
Add the ability to override a token. Previously this resulted in a stack overflow error when resolving the token. ```ts const answerProvider = rootInjector .provideValue('answer', 42) .provideValue('answer', '42'); expect(answerProvider.resolve('answer')).eq('42'); expect(typeof answerProvider.resolve('answer')).eq('string'); ``` With this functionality in place, it is now also possible to add decorators to existing dependencies. See readme for more info.
- Loading branch information
Showing
8 changed files
with
126 additions
and
25 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,17 @@ | ||
import { InjectableClass, InjectableFunction } from './Injectable'; | ||
import { InjectionToken } from './InjectionToken'; | ||
import { Scope } from './Scope'; | ||
import { TChildContext } from './TChildContext'; | ||
|
||
export interface Injector<TContext = {}> { | ||
injectClass<R, Tokens extends InjectionToken<TContext>[]>(Class: InjectableClass<TContext, R, Tokens>): R; | ||
injectFunction<R, Tokens extends InjectionToken<TContext>[]>(Class: InjectableFunction<TContext, R, Tokens>): R; | ||
resolve<Token extends keyof TContext>(token: Token): TContext[Token]; | ||
provideValue<Token extends string, R>(token: Token, value: R): Injector<{ [k in Token]: R } & TContext>; | ||
provideValue<Token extends string, R>(token: Token, value: R) | ||
: Injector<TChildContext<TContext, R, Token>>; | ||
provideClass<Token extends string, R, Tokens extends InjectionToken<TContext>[]>(token: Token, Class: InjectableClass<TContext, R, Tokens>, scope?: Scope) | ||
: Injector<{ [k in Token]: R } & TContext>; | ||
: Injector<TChildContext<TContext, R, Token>>; | ||
provideFactory<Token extends string, R, Tokens extends InjectionToken<TContext>[]>(token: Token, factory: InjectableFunction<TContext, R, Tokens>, scope?: Scope) | ||
: Injector<{ [k in Token]: R } & TContext>; | ||
: Injector<TChildContext<TContext, R, Token>>; | ||
dispose(): Promise<void>; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export type TChildContext<TParentContext, TProvided, CurrentToken extends string> = { | ||
[K in keyof (TParentContext & { [K in CurrentToken]: TProvided })]: K extends CurrentToken ? TProvided : K extends keyof TParentContext ? TParentContext[K]: never; | ||
}; |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// error: "Type 'string' is not assignable to type 'number'" | ||
|
||
import { rootInjector } from '../src/index'; | ||
|
||
const fooProvider = rootInjector | ||
.provideValue('foo', 42) | ||
.provideValue('foo', 'bar'); | ||
|
||
const foo: number = fooProvider.resolve('foo'); |
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