@smertos/tydi
TypeScript icon, indicating that this package has built-in type declarations

1.0.3 • Public • Published

typescriptDI

npm version why not

tydi is a simple Dependency Injection helper, that adds 2 simple decorators for easy property/constructor parameters injection of decorated classes or any other kind of values using assigned custom keys.

Example

First you'll need to define some service.

import { Service } from '@smertos/tydi';

@Service()
class AuthService {
	
    isLoggedIn(): boolean {
    	return ...;
    }
    	
}

Then somewhere you need this service, you'll need to inject the service.

import { Container, Inject } from '@smertos/tydi';
import { AuthService } from '...';

class LoginComponent {
	
    @Inject() authService: AuthService;
    
    logIn(): void {
    	if (this.authService.isLoggedIn) return;
        
        ...
    }

}

If you do not manage instantiation of class (i.e. React components) and do not pass any arguments, then you can use constructor parameter injection (works like in Angular 2+). Access modificators can be used with injected parameters without any problems.

class LoginComponent {
	
    constructor(private authService: AuthService) {}
    
    logIn(): void {
    	if (this.authService.isLoggedIn) return;
        
        ...
    }

}

If you wish to do instantiation yourself as well, as pass parameters to constructor, you can do it as it is supported, but TypeScript may think about it otherwise.

Readme

Keywords

none

Package Sidebar

Install

npm i @smertos/tydi

Weekly Downloads

1

Version

1.0.3

License

MIT

Unpacked Size

75.7 kB

Total Files

23

Last publish

Collaborators

  • smertos