@taqtile/typedi-extensions
TypeScript icon, indicating that this package has built-in type declarations

0.7.1 • Public • Published

Typedi Extension

Typedi's extensions.

Contents

Injectable decorator

Injectable is a decorator that makes a class dependency injectable outside of Typedi's ecosystem.

It's specially useful when you want DI in classes with their own life-cycles (i.e React components).

import { Container, Service } from 'typedi';
import { Injectable } from '@taqtile/typedi-extensions';

@Service()
class AService {
  doSomething() {
    /* ... */
  }
}

Container.set('namedValue', 'some value')

@Injectable()
export class Template extends Component<any, any> {
  constructor(
    props?: any, // passed by React's engine
    context?: any, // passed by React's engine
    aService?: AService, // injected using Typedi
    @Inject('namedValue') strValue: string, // Typedi's named injection
  ) {
    super(props, context);
    aService.doSomething();
    console.log(strValue) // 'some value'
  }
}

!!! Warning !!!

  • Currently Injectable does not support member injection using Inject (though you can use it in class constructor)
@Injectable()
class SomeClass {

  @Inject('notInjected') failsToInject;

  constructor(@Inject('injected') injected) { }
}

Readme

Keywords

none

Package Sidebar

Install

npm i @taqtile/typedi-extensions

Weekly Downloads

32

Version

0.7.1

License

MIT

Unpacked Size

6.53 kB

Total Files

10

Last publish

Collaborators

  • taqtile
  • tibawatanabe