cap-js / cds-typer

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

[BUG] [REGRESSION] TS2612 - Property 'ID' will overwrite the base property in '_cuidAspect<TBase> (v0.23.0) #274

Closed geert-janklaps closed 1 month ago

geert-janklaps commented 2 months ago

Is there an existing issue for this?

Nature of Your Project

TypeScript

Current Behavior

As of release v0.23.0 generated types using cuidAspect produce TS2612 errors. (strict tsconfig) This issue might be closely related to #273. image

The fix however is different, in the compilerOptions you need to disable useDefineForClassFields.

{
  "ts-node": {
    "files": true
  },
  "compilerOptions": {
    "paths": {
      "#cds-models/*": [
        "./@cds-models/*"
      ]
    },
    "rootDir": "./",
    "target": "ESNext",
    "module": "NodeNext",
    "moduleResolution": "NodeNext",
    "sourceMap": true,
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "noImplicitOverride": false,
    "skipLibCheck": true,
    "useDefineForClassFields": false
  }
}

Expected Behavior

The generated types not to produce errors in strict mode

Steps To Reproduce

No response

Environment

@cap-js/asyncapi: 1.0.1
@cap-js/cds-typer: 0.23.0
@cap-js/openapi: 1.0.4
@cap-js/sqlite: 1.7.3
@sap/cds: 7.9.3
@sap/cds-common-content: 1.4.0
@sap/cds-compiler: 4.9.6
@sap/cds-dk: 7.9.5
@sap/cds-dk (global): 8.0.2
@sap/cds-fiori: 1.2.7
@sap/cds-foss: 5.0.1
@sap/cds-hana: 2.0.0
@sap/cds-mtxs: 1.18.2
@sap/eslint-plugin-cds: 2.7.0
Node.js: v20.14.0

Repository Containing a Minimal Reproducible Example

No response

Anything else?

No response

cpf-hse commented 1 month ago

Thank you @geert-janklaps for the mentioned fix. Had the same issue: error TS2612: Property 'createdAt' will overwrite the base property in '_managedAspect<TBase>.managed & object'. If this is intentional, add an initializer. Otherwise, add a 'declare' modifier or remove the redundant declaration.

@cap-js/asyncapi: 1.0.2 @cap-js/cds-typer: 0.24.0 @cap-js/cds-types: 0.6.3 @cap-js/openapi: 1.0.4 @cap-js/postgres: 1.9.1 @cap-js/sqlite: 1.7.3 @sap/cds: 8.0.4 @sap/cds-compiler: 5.0.6 @sap/cds-dk (global): 8.0.3 @sap/cds-fiori: 1.2.7 @sap/cds-foss: 5.0.1 @sap/cds-mtxs: 2.0.3 @sap/eslint-plugin-cds: 3.0.4 Node.js: v20.15.1

daogrady commented 1 month ago

Hi Geert-Jan,

thanks for reporting this issue! I am unable to reproduce the problem with the information I have so far.

With the following tsconfig.json:

{
"compilerOptions": {
  "paths": {
    "#cds-models/*": [
      "./@cds-models/*"
    ]
  },
  "rootDir": "./",
  "target": "ESNext",
  "module": "NodeNext",
  "moduleResolution": "NodeNext",
  "sourceMap": true,
  "esModuleInterop": true,
  "forceConsistentCasingInFileNames": true,
  "strict": true,
  "noImplicitOverride": false,
  "skipLibCheck": true,
  "useDefineForClassFields": true
}
}

and this minimal model:

using { cuid } from '@sap/cds/common';
namespace my.bookshop;

entity Book: cuid {
    key ID : Integer;
    title : String;
    author : String;
    stock : Integer;
    price : Decimal(9,2);
}

I am running tsc (5.5.4) in my project root and I do not see any errors and the transpiler emits .js files just fine.

Could you please expand your sample with a minimal model I can reproduce the issue with? Thanks!

Best, Daniel

cpf-hse commented 1 month ago

I can support here as well as I also had this issue.

Steps to reproduce

cds init typerbug
cds add typescript
npm install

The generated tsconfig file provided by CAP looks like this

{
  "compilerOptions": {
    "target": "ESNext",
    "module": "NodeNext",
    "moduleResolution": "NodeNext",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "skipLibCheck": true,
    "sourceMap": true,
    "allowJs": true,
    "paths": {
      "#cds-models/*": [
        "./@cds-models/*/index.ts"
      ]
    }
  }
}

Now add the following schema.cds to your db folder

using {managed} from '@sap/cds/common';

namespace typer.bug;

entity ManagedEntity : managed {
    key id        : UUID;
        something : String(15);
}

Now create a service.cds in the srv folder

using {typer.bug as service} from '../db/schema';

service ManagedService {
    entity ManagedEntity as projection on service.ManagedEntity;
}

Run build

cds-ts build

Output

Error: @cds-models/ManagedService/index.ts(7,5): error TS2612: Property 'createdAt' will overwrite the base property in '_managedAspect<TBase>.managed & object'. If this is intentional, add an initializer. Otherwise, add a 'declare' modifier or remove the redundant declaration.
@cds-models/ManagedService/index.ts(11,5): error TS2612: Property 'createdBy' will overwrite the base property in '_managedAspect<TBase>.managed & object'. If this is intentional, add an initializer. Otherwise, add a 'declare' modifier or remove the redundant declaration.
@cds-models/ManagedService/index.ts(12,5): error TS2612: Property 'modifiedAt' will overwrite the base property in '_managedAspect<TBase>.managed & object'. If this is intentional, add an initializer. Otherwise, add a 'declare' modifier or remove the redundant declaration.
@cds-models/ManagedService/index.ts(16,5): error TS2612: Property 'modifiedBy' will overwrite the base property in '_managedAspect<TBase>.managed & object'. If this is intentional, add an initializer. Otherwise, add a 'declare' modifier or remove the redundant declaration.
daogrady commented 1 month ago

Hi Christian,

thanks for adding the reproduction steps! I can now see the error and will look into it.

Best, Daniel

daogrady commented 1 month ago

Hi everyone,

I have prepared an experimental fix for this issue that will add the declare modifier to all properties. Would you mind trying out this proposal to see if it matches your use case and works in combination with your tsconfig.json?

Best, Daniel

cpf-hse commented 1 month ago

Hi @daogrady , thank you for providing a potential fix. How would I be able to test it ๐Ÿ˜“ ๐Ÿ˜„ ?

daogrady commented 1 month ago

Hi Christian,

sorry, I forgot to attach the relevant wiki page.

Best, Daniel

cpf-hse commented 1 month ago

@daogrady I try to do it this week. I had a covid infection and was not able to work until today. I do by best to test it by the end of this week.

daogrady commented 1 month ago

Hi Christian,

sorry to hear and hope you recover quickly! ๐Ÿ˜• I have merged the fix in question for a related problem. So I will for now assume the problem is fixed (with the next release). If you notice the problem resurfacing, feel free to reopen this issue.

Best, Daniel