From b19454b7943521efec9fb3403ff080106a2ef0d4 Mon Sep 17 00:00:00 2001 From: Nico Rehwaldt Date: Sun, 4 Feb 2024 19:02:33 +0100 Subject: [PATCH] docs: document typed usage --- README.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/README.md b/README.md index 25bae9b..6de9282 100644 --- a/README.md +++ b/README.md @@ -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'); + +// typed as +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(...); + +const hifiComponent = injector.get('hifiComponent'); +// typed as +``` + + ## 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.