cap-js / cds-types

Type definitions for CDS
Apache License 2.0
10 stars 10 forks source link

[BUG] TS2339: Property 'on' does not exist on type. #318

Open sebastien-savalle opened 2 weeks ago

sebastien-savalle commented 2 weeks ago

Is there an existing issue for this?

Current Behavior

We observed a weird behavior when migrating to cds 8 and cds-types 0.7.0.

Custom handler cannot be registered properly. A typescript error is thrown.

error TS2339: Property 'on' does not exist on type 'AdminService'.

import { ApplicationService } from '@sap/cds';

export default class AdminService extends ApplicationService {
  init() {
    this.on('health', () => {
      return { status: 'UP' };
    });
  }
}

But it works when this is cast to ApplicationService.

import { ApplicationService } from '@sap/cds';

export default class AdminService extends ApplicationService {
  init() {
    (this as ApplicationService).on('health', () => {
      return { status: 'UP' };
    });
  }
}

Expected Behavior

No response

References

https://pages.github.tools.sap/cap/docs/tools/cds-typer#typer-top-level-imports

Versions

backend https://github.com/<your/repo>
@cap-js/asyncapi 1.0.2
@cap-js/audit-logging 0.8.1
@cap-js/cds-types 0.7.0
@cap-js/db-service 1.14.1
@cap-js/hana 1.4.1
@cap-js/openapi 1.0.7
@cap-js/sqlite 1.7.6
@cap-js/telemetry 1.0.1
@sap/cds 8.4.0
@sap/cds-compiler 5.4.0
@sap/cds-dk 8.4.0
@sap/cds-dk (global) 8.4.0
@sap/cds-fiori 1.2.7
@sap/cds-foss 5.0.1
@sap/cds-mtxs 2.3.0
@sap/eslint-plugin-cds 3.0.4
Node.js v20.15.1

Anything else? Logs?

No response

sebastien-savalle commented 2 weeks ago

Same code works with cds-types 0.6.5 :(

daogrady commented 1 week ago

Hi Sébastien,

thanks for reporting this issue! This looks a bit like TS is trying to infer the type from the implementation files, which would imply you don't have cds-types installed in your project. Please note that since cds8, @cap-js/cds-types has to be installed explicitly and is no longer pulled in from @sap/cds. Did you add @cap-js/cds-types as devDependency to your project and install it?

Best, Daniel

daogrady commented 1 week ago

Actually, I am pretty sure this is a duplicate of https://github.com/cap-js/cds-types/issues/251

If you do have cds-types installed, can you please run (p)npm rebuild in your project and see, if that fixes the problem?

sebastien-savalle commented 1 week ago

Hi Daniel,

Yes, we do have cds-types installed as you can see in the summary. We use pnpm, so this might be similar to the issue you mentioned.

But, this works well with version 0.6.5.

daogrady commented 1 week ago

Hi Sébastien,

thanks for checking! Please also check out my other suggestion.

Best, Daniel

hakimio commented 1 week ago

pnpm rebuild doesn't fix the issue. Most likely because of the issue with pnpm: https://github.com/cap-js/cds-types/issues/251#issuecomment-2468470167

daogrady commented 5 days ago

Hi Sébastien,

can you please check out @hakimio's suggestion and see if that solves the problem for you? If it does, could you please also elaborate a bit on your setup? Do you use a monorepo? If so, do the submodules of that monorepo have a dependency on cds-types?

Best, Daniel