Type-graphql: Question: Workaround for TS2564 (strictPropertyInitialization)

Created on 21 May 2018  路  1Comment  路  Source: MichalLytek/type-graphql

When running with typescript >= 2.7 and strictPropertyInitialization or strict compiler options enabled, the schema types like:

import { Field, ObjectType } from "type-graphql";

@ObjectType({ description: "User profile" })
export class Profile {
  @Field()
  firstname: string;

  @Field()
  lastname: string;

  @Field()
  bio: string;
}

will cause compiler errors like:

Error:(6, 3) TS2564: Property 'firstname' has no initializer and is not definitely assigned in the constructor.

Is there a good workaround for this that doesn't involve writing an unnecessary constructor or disabling this check for the entire project?

The root cause of this is my biggest gripe with the typescript decorators in that they cannot be read at run-time when applied to interfaces.

Question Solved

Most helpful comment

export class Profile {
  @Field()
  firstname!: string;

  @Field()
  lastname!: string;

  @Field()
  bio!: string;
}

>All comments

export class Profile {
  @Field()
  firstname!: string;

  @Field()
  lastname!: string;

  @Field()
  bio!: string;
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

tafelito picture tafelito  路  3Comments

Tybot204 picture Tybot204  路  3Comments

MichalLytek picture MichalLytek  路  4Comments

maplesteve picture maplesteve  路  3Comments

winuxue picture winuxue  路  4Comments