Skip to main content

TypeDI++

Elegant Dependency Injection in JavaScript and TypeScript.

Injection without the confusion.

All through an expressive, decorator-based syntax.

@Service([USERNAME, POSTS])
export class FeedService extends EventTarget {
constructor (private username: string, private posts: Post[]) {
super();
}

addPost (post: string) {
this.posts.push({ username: this.username, post });
this.dispatchEvent(new CustomEvent('post-added'));
}
}
Wondering how this example works?
This example uses a combination of the above code and React.
The implementation of this example can be seen here.

freshgumhello! :-)

freshgumtry typing here!

Angular-esque API

TypeDI lets you use operators such as SkipSelf, Self,
and Optional to change how dependencies are retrieved.

const USERNAME = new Token<string>();

@Service({ scope: 'transient' }, [
[USERNAME, Optional()]
])
export class GreetingService {
constructor (private userName?: string) { }

getGreeting () {
return `Hello, ${this.userName ?? 'person'}!`;
}
}

Hello, person!