# nestjs-typesafe-decorators

> Typesafe @Inject decorator for Nest

Latest version **2.0.3** (published 2023-05-06) · MIT license · 0 weekly downloads

## Install

```sh
npm install nestjs-typesafe-decorators
pnpm add nestjs-typesafe-decorators
yarn add nestjs-typesafe-decorators
bun add nestjs-typesafe-decorators
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.3 |
| Published | 2023-05-06 |
| First published | 2023-04-21 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 7.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Tamás Hegedűs |
| Maintainers | sorgloomer |
| Keywords | typescript, typesafe, decorator, ioc, inference, inject, nestjs, nest |

## Links

- npm: https://www.npmjs.com/package/nestjs-typesafe-decorators
- Homepage: https://github.com/sorgloomer/ts-typesafe-decorators
- Funding: https://github.com/sponsors/sorgloomer
- npm.io page: https://npm.io/package/nestjs-typesafe-decorators

## Dependencies (2)

- [tslib](https://npm.io/package/tslib.md) ^2.5.0
- [typesafe-decorators](https://npm.io/package/typesafe-decorators.md) ^2.0.2

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 2.0.3 (latest) — 2023-05-06
- 2.0.2 — 2023-05-06
- 2.0.1 — 2023-05-06
- 2.0.0 — 2023-05-06
- 1.1.0 — 2023-04-24
- 1.0.3 — 2023-04-21
- 1.0.2 — 2023-04-21
- 1.0.1 — 2023-04-21
- 1.0.0 — 2023-04-21

## README

# nestjs-typesafe-decorators

<p>
  <a href="https://github.com/sorgloomer/ts-typesafe-decorators/actions?query=branch%3Amaster"><img src="https://github.com/sorgloomer/ts-typesafe-decorators/actions/workflows/test.yml/badge.svg?event=push&branch=master" alt="Test" /></a>
  <a href="https://opensource.org/licenses/MIT" rel="nofollow"><img src="https://img.shields.io/github/license/sorgloomer/ts-typesafe-decorators" alt="License"></a>
</p>

<div>
  <a href="https://github.com/sorgloomer/ts-typesafe-decorators/tree/master/packages/nestjs-typesafe-decorators">GitHub</a>
  <span>&nbsp;&nbsp;•&nbsp;&nbsp;</span>
  <a href="https://www.npmjs.com/package/nestjs-typesafe-decorators">npm</a>
  <span>&nbsp;&nbsp;•&nbsp;&nbsp;</span>
  <a href="https://github.com/sorgloomer/ts-typesafe-decorators/issues">Issues</a>
  <span>&nbsp;&nbsp;•&nbsp;&nbsp;</span>
  <a href="https://twitter.com/hege_hegedus">@hege_hegedus</a>
  <br />
</div>

# Intro

Typescript 5 made it possible for legacy experimental decorators to check the types of the things they are applied to.

This repository contains helper libraries to enforce the correct types of injected services at compile time, for your
favorite ioc container.


### Usage

Install

```shell
npm i --save nestjs-typesafe-decorators
```

Use the `TypedInjectionToken<IService>` type to annotate your injection tokens

```typescript
export const TOKEN_FOO: TypedInjectionToken<IFooService> = 'TOKEN_FOO';
```

Replace `@Inject(...)` decorators with `@TypedInject(...)`

```typescript
@Injectable()
export class Service {
  constructor(
    @TypedInject(TOKEN_FOO)
    private readonly fooService: IFooService,
  ) {}
}
```

Wrap your token based providers in `typedProvider(...)`

```typescript
@Module({
  providers: [
    Service,
    typedProvider({ provide: TOKEN_FOO, useClass: FooService }),
  ]
})
class AppModule {}
```


### A more complete example

```typescript
import { Injectable, Logger, Module } from '@nestjs/common';
import { TypedInject, TypedInjectionToken, typedProvider } from 'nestjs-typesafe-decorators';

export interface IFooService { foo(): string; }
export interface IBarService { bar(): string; }

export const TOKEN_FOO = 'TOKEN_FOO' as TypedInjectionToken<IFooService>;
export const TOKEN_BAR = 'TOKEN_BAR' as TypedInjectionToken<IBarService>;

@Injectable()
export class Service {
  constructor(
    private readonly logger: Logger,

    @TypedInject(TOKEN_FOO)
    private readonly fooService: IFooService,

    @TypedInject(TOKEN_FOO)
//  ^^^^^^^^^^^^^^^^^^^^^^^
    private readonly barService: IBarService,
  ) {}
}

@Injectable() class FooService implements IFooService { foo(): string { return '' }; }
@Injectable() class BarService implements IBarService { bar(): string { return '' }; }

@Module({
  providers: [
    Service,
    typedProvider({ provide: TOKEN_FOO, useClass: FooService }),
    typedProvider({ provide: TOKEN_BAR, useClass: FooService }),
//                                      ^^^^^^^^
  ]
})
class AppModule {}
```

Take a look at the [`examples`](../../examples) folder.


### Future

Eventually I'd love to see stricter types integrated into TypeScript, Nest and Inversify. Until that,
we can use these libraries.

---
_Source: https://npm.io/package/nestjs-typesafe-decorators · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
