typesafe-decorators

2.0.2 • Public • Published

typesafe-decorators

Test License

GitHub   •   npm   •   Issues   •   @hege_hegedus

Intro

Typescript 5 made it possible for legacy experimental decorators to check the types of the parameters 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.

Example

import { TypedPropertyDecorator, TypedParameterDecorator } from 'typesafe-decorators';

declare const StringLogger: TypedPropertyDecorator<string, 'get'>;
declare const EnumValidator: TypedPropertyDecorator<'foo' | 'bar' | 'baz', 'set'>
 & TypedParameterDecorator<'foo' | 'bar' | 'baz', 'set'>;

class Foo {
  @StringLogger // OK
  private x1!: 'a' | 'b';

  @StringLogger
  // ^^^^^^^^^^  Type of property is not assignable to type of decorator
  private x2!: number;

  @EnumValidator   // OK
  private x3!: string;

  @EnumValidator
  // ^^^^^^^^^^^  Type of decorator is not assignable to type of property
  private x4!: 'foo' | 'bar';

  private foo(
    @EnumValidator
    p1: string,
    @EnumValidator
    // ^^^^^^^^^^^  Type of decorator is not assignable to type of parameter
    p2: 'foo' | 'bar',
  ) {}
}

See also

Package Sidebar

Install

npm i typesafe-decorators

Weekly Downloads

159

Version

2.0.2

License

MIT

Unpacked Size

7.88 kB

Total Files

5

Last publish

Collaborators

  • sorgloomer