cds-typer icon indicating copy to clipboard operation
cds-typer copied to clipboard

[BUG] Error when generating TS types for Auxiliary type in CDS

Open gaurav-kumar04 opened this issue 11 months ago • 2 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

Nature of Your Project

TypeScript

Current Behavior

When a custom type in CDS is created by referring to an entity(also called auxiliary type) then the cds-typer fails to generate a valid TS type. Custom type example:

entity Airlines {
    key AirlineID : String(3);
        Name      : String(40);
}

type AirlineType : Airlines {} // Auxiliary type;

Auxiliary type details: https://cap.cloud.sap/docs/cds/compiler-v2#_2-only-entities-from-same-service-allowed

Generated TS type

export function _AirlineAspect<TBase extends new (...args: any[]) => object>(Base: TBase) {
  return class Airline extends Base {
        AirlineID?: string;
        Name?: string | null;
      static actions: {
      }
  };
}
export class Airline extends _AirlineAspect(__.Entity) {}
Object.defineProperty(Airline, 'name', { value: 'Airlines' })
export class Airlines extends Array<Airline> {}
Object.defineProperty(Airlines, 'name', { value: 'Airlines' })

export function _AirlineTypeAspect<TBase extends new (...args: any[]) => object>(Base: TBase) {
  return class AirlineType extends Base {
      static actions: {
      }
  };
}

/* ERROR: Cannot find name '_AirlinesAspect'. Did you mean '_AirlineAspect' */
export class AirlineType extends _AirlinesAspect(_AirlineTypeAspect(__.Entity)) {}  // <--- Error
Object.defineProperty(AirlineType, 'name', { value: 'AirlineType' })

Expected Behavior

Generated type should be:

export function _AirlineAspect<TBase extends new (...args: any[]) => object>(Base: TBase) {
  return class Airline extends Base {
        AirlineID?: string;
        Name?: string | null;
      static actions: {
      }
  };
}
export class Airline extends _AirlineAspect(__.Entity) {}
Object.defineProperty(Airline, 'name', { value: 'Airlines' })
export class Airlines extends Array<Airline> {}
Object.defineProperty(Airlines, 'name', { value: 'Airlines' })

export function _AirlineTypeAspect<TBase extends new (...args: any[]) => object>(Base: TBase) {
  return class AirlineType extends Base {
      static actions: {
      }
  };
}

/* It should refer to `_AirlineAspect` instead of `_AirlinesAspect`  */
export class AirlineType extends _AirlinesAspect(_AirlineTypeAspect(__.Entity)) {} 
Object.defineProperty(AirlineType, 'name', { value: 'AirlineType' })

Steps To Reproduce

  1. Setup a TS project with cds-typer enabled
  2. Create a .cds file
  3. Add the following code:
entity Airlines {
    key AirlineID : String(3);
        Name      : String(40);
}

type AirlineType : Airlines {} // Auxiliary type;
  1. Execute the command tsc --noEmit, it will show the error in the generated file.

Environment

- **OS** : MacOS
- **Node** : 18.18.0
- **npm** : 9.8.0
- **cds-typer** : 0.18.2 
- **cds** : 7.8.0

Repository Containing a Minimal Reproducible Example

No response

Anything else?

No response

gaurav-kumar04 avatar Mar 25 '24 19:03 gaurav-kumar04