Skip to content

schema-codegen TypeScript output missing initializers in some fields; may not compile #92

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
cdata opened this issue Sep 27, 2020 · 0 comments

Comments

@cdata
Copy link

cdata commented Sep 27, 2020

This issue is being filed after some discussion with @endel in Discord. Some of the bug will be a rehash of that discussion.

Background

I'm working on a TypeScript-based game. I experimented with using schema-codegen to produce TypeScript output so that my client could know the type of room.state as it is defined in the corresponding server modules.

Problem

The generated classes are missing initializers for fields with primitive types. For example, given the input:

class ArenaState extends Schema {
  @type({ map: Player })
  players = new MapSchema<Player>();

  @type('number')
  turn = 0;

  @type('boolean')
  waitingForPlayers: boolean = true;
}

I get the following generated output:

export class ArenaState extends Schema {
    @type({ map: Player }) public players: MapSchema<Player> = new MapSchema<Player>();
    @type("number") public turn: number;
    @type("boolean") public waitingForPlayers: boolean;
}

See how the initializers are removed from the turn and waitingForPlayers fields? The main problem with this output is that it isn't valid TypeScript (or at least, not for basic configurations of the TypeScript compiler). For example, consider this trivial case: https://www.typescriptlang.org/play?#code/MYGwhgzhAEBiD29oG8BQ0PQA4FcBGIAlsNHmAE4Bc0AdjgLZ4Cm5A3KgL5A

Potential remedies

@endel pointed out that if I could afford to consume the Schema-based classes as a shared module, it would not be necessary to use schema-codegen to produce a client version. This is what I decided to do, and it made a lot of sense to me because it gives me the most flexibility with my tsconfig.json settings.

After some discussion with @endel he suggested that this code might compile if "strictNullChecks": false is set in tsconfig.json. So, perhaps all that is needed is a note about this in the README (apologies if I missed an existing note about this).

Yet another remedy would be to change the TypeScript output produced by schema-codegen, either to make it more faithfully reflect the source it was generated from, or to at least make it more likely to be compiled by a wider range of tsconfig.json configurations.

Thanks a lot for the help so far, and I look forward to discussion about this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant