2.0.2 • Published 1 year ago

typesafe-decorators v2.0.2

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

typesafe-decorators

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