balmbees / dynamo-types

Typescript AWS DynamoDB ORM

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

no initializer error

mwildehahn opened this issue · comments

I'm running into the following error when trying to define a class:

import { Decorator, Query, Table } from "dynamo-types";

@Decorator.Table({ name: `users` })
export default class User extends Table {
  @Decorator.HashPrimaryKey("id")
  public static readonly primaryKey: Query.HashPrimaryKey<User, string>;

  @Decorator.Writer()
  public static readonly writer: Query.Writer<User>;

  @Decorator.Attribute()
  public id: string;

  @Decorator.Attribute()
  public name: string;

  @Decorator.Attribute()
  public email: string;

  @Decorator.Attribute()
  public image: string;
}

The error is on any of the @Decorator.Attribute fields:

Property 'image' has no initializer and is not definitely assigned in the constructor.ts(2564)

If I define a constructor though, it says I don't conform to the interface defined here:

new(): T;
.

I was looking at extending that interface to new(...args: any[]) but was curious why I would need to do that.

commented

it's because of TS strict mode, where forcing code to have initialization code of constructor.
typically in ORM though, (i meant all kind of, not just this library) this restriction is not valid.
the intention of this limit is to make sure class instance being created with all the member variable being initialized, but in ORM we "deserialize" DB schema into js object. thus we don't use those constructor anyway - you can checkout TypeORM for same issue.

in short, i can allow constructor but that's not gonna solve issue of data => JS scenario.
i recommend to do public image!: string and create static method like public static async create() if you need such an helper method.