Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow to type available services #33

Merged
merged 2 commits into from
Feb 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,38 @@ const injector = new Injector([
```


### Type Service Retrival

[`didi`](https://github.com/nikku/didi) ships type declarations that allow you to use it in a type safe manner.

#### Explicit Typing

Pass a type attribute to `Injector#get` to retrieve a service as a known type:

```typescript
const hifiComponent = injector.get<HifiComponent>('hifiComponent');

// typed as <HifiComponent>
hifiComponent.toggle();
```

#### Implicit Typing

Configure the `Injector` through a service map and automatically cast services
to known types:

```typescript
type ServiceMap = {
'hifiComponent': HifiComponent
};

const injector = new Injector<ServiceMap>(...);

const hifiComponent = injector.get('hifiComponent');
// typed as <HifiComponent>
```


## Credits

This library builds on top of the (now unmaintained) [node-di][node-di] library. `didi` is a maintained fork that adds support for ES6, the minification safe array notation, and other features.
Expand Down
10 changes: 9 additions & 1 deletion lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,21 @@ export type LocalsMap = {

export type ModuleDefinition = ModuleDeclaration;

export class Injector {

export class Injector<
ServiceMap = null
> {

/**
* Create an injector from a set of modules.
*/
constructor(modules: ModuleDefinition[], parent?: InjectorContext);

/**
* Return a named service, looked up from the existing service map.
*/
get<Name extends keyof ServiceMap>(name: Name): ServiceMap[Name];

/**
* Return a named service, and throws if it is not found.
*/
Expand Down
33 changes: 33 additions & 0 deletions test/integration/ts.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,39 @@ describe('typed', function() {
expect(injector).to.exist;
});


it('should offer typed injections', function() {

// given
type ServiceMap = {
'foo': 1,
'bar': 'BAR'
};

// when
const injector = new Injector<ServiceMap>([
{
foo: [ 'value', 1 ],
bar: [ 'value', 'BAR' ]
}
]);

// then
const foo = injector.get('foo');
expect(foo).to.eql(1);

const bar = injector.get('bar');
expect(bar).to.eql('BAR');

const baz = injector.get('baz', false);
expect(baz).not.to.exist;

// illegal usage, but if you think you know better
// we still accept it
const boolBar = injector.get<boolean>('bar');
expect(boolBar).to.exist;
});

});


Expand Down