cap-js / cds-typer

CDS type generator for JavaScript
Apache License 2.0
26 stars 8 forks source link

`Plural.drafts` could be `typeof Plural` #163

Open daogrady opened 7 months ago

daogrady commented 7 months ago

Currently, both the singular and the plural form of entities have a reference to .drafts. This makes sense as they point to the same CSN entity during runtime and should be treated the same. So:

class Singular {
  static drafts: typeof Singular
}

class Plural extends Array<Singular> {
  static drafts: typeof Singular
}

This leads to confusion when the type should not actually matter:

SELECT.from(b ? Plural : Plural.drafts)  // Singular | Plural -> no proper type support

Find out if there is a compelling argument against

class Plural extends Array<Singular> {
  static drafts: typeof Plural
}

and if not implement this behaviour.

geert-janklaps commented 5 months ago

Hi @daogrady,

This actually is causing issues when registering handlers, if you register the handler with the plural of the drafts it's typed as an object. At runtime the value is not an object but an array. image

Cheers, Geert-Jan

geert-janklaps commented 5 months ago

Pull request #192 created to resolve this issue. (ran unit tests & did the same manual change in my running project to validate)