Skip to content
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

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

Open
1 task done
sebastien-savalle opened this issue Nov 6, 2024 · 10 comments
Open
1 task done

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

sebastien-savalle opened this issue Nov 6, 2024 · 10 comments
Assignees
Labels
bug Something isn't working

Comments

@sebastien-savalle
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

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 sebastien-savalle added the bug Something isn't working label Nov 6, 2024
@sebastien-savalle
Copy link
Author

Same code works with cds-types 0.6.5 :(

@daogrady
Copy link
Contributor

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
Copy link
Contributor

daogrady commented Nov 11, 2024

Actually, I am pretty sure this is a duplicate of #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
Copy link
Author

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
Copy link
Contributor

Hi Sébastien,

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

Best,
Daniel

@hakimio
Copy link
Contributor

hakimio commented Nov 15, 2024

pnpm rebuild doesn't fix the issue. Most likely because of the issue with pnpm: #251 (comment)

@daogrady
Copy link
Contributor

daogrady commented Nov 18, 2024

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

@sebastien-savalle
Copy link
Author

Hi Daniel,

First of all regarding our project setup:

  • We have a monorepo managed by pnpm
  • We added cds-types as a dev dependency in every submodules and also in root package

I tried a few things to make it work:

  1. As suggested, I put @cap-js/cds-types as types:
image
  1. Then, I tried with "./node_modules/@cap-js/cds-types"
image

3 Finally, I found another workaround by adding cds-types as typeRoots like
image

@hakimio
Copy link
Contributor

hakimio commented Nov 26, 2024

@sebastien-savalle how does your tsconfig look like? Does the types config work if you set moduleResolution to NodeNext?

@sebastien-savalle
Copy link
Author

sebastien-savalle commented Nov 27, 2024

No, this does not work even with moduleResolution set to NodeNext

And pnpm rebuild is sometimes helpful when everything looks broken.

tsconfig:

{
  "compilerOptions": {
    "target": "es2019",
    "module": "nodenext",
    "outDir": "./gen",
    "rootDir": "./",
    "baseUrl": ".",
    "moduleResolution": "nodenext",
    "skipLibCheck": true,
    "preserveConstEnums": true,
    "sourceMap": true,
    "allowJs": true,
    "strict": true,
    "strictNullChecks": true,
    "strictPropertyInitialization": false,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "noImplicitAny": false,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "types": [
      "node",
      "jest",
      "express"
    ],
    "typeRoots": [
      "./types",
      "./node_modules/@types",
      "./node_modules/@cap-js/cds-types"
    ]
  },
  "include": [
    "../.eslintrc.json",
    "./srv/**/*.ts",
    "./types/**/**.d.ts",
    "./test/**/*.ts"
  ]
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants