balmbees / dynamo-types

Typescript AWS DynamoDB ORM
231 stars 18 forks source link

no initializer error #62

Closed mwildehahn closed 4 years ago

mwildehahn commented 4 years ago

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: https://github.com/balmbees/dynamo-types/blob/d6befcca35f5a82f2137003720b21a473c091dab/src/table.ts#L77.

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

breath103 commented 4 years ago

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.

mwildehahn commented 4 years ago

Ah that makes sense. Thanks!

On Tue, May 12, 2020 at 7:25 PM Kurt notifications@github.com wrote:

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 method 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.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/balmbees/dynamo-types/issues/62#issuecomment-627705763, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAFMAUN5SRFOMQRM7GLVPXTRRIAJVANCNFSM4M7JSE4A .