Skip to content

Commit

Permalink
docs: document typed usage
Browse files Browse the repository at this point in the history
  • Loading branch information
nikku committed Feb 4, 2024
1 parent 01f9eaf commit b19454b
Showing 1 changed file with 32 additions and 0 deletions.
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

0 comments on commit b19454b

Please sign in to comment.