cancel
Showing results for 
Search instead for 
Did you mean: 

Error when using TypeScript with CAP

johnmurray
Participant

Hi,

I've migrated one of my CAP projects to TypeScript and all is going well, however in my code I use quite a lot of SELECT.from(table).where(…) which of course TypeScript doesn’t like. I’m importing the types as per import { SELECT } from "@sap/cds/apis/ql" . Now however, when I run my code with cds-ts watch I receive this error Error: Cannot find module '@sap/cds/apis/ql'. Does anyone know how to resolve this? The file is there, and TypeScript is happy with types, so I’m at a loss as to what the problem is. Other imports from cds/apis work too, such as import { Service } from "@sap/cds/apis/services" so it just seems to be the cql stuff that doesn’t work.

I can swap the import statement to

const cds = require("@sap/cds");
const { SELECT } = cds.ql;

which then allows me to run my service at least, but then I lose all TypeScript code help because SELECT becomes type 'any'.

So summarise:

import { SELECT } from "@sap/cds/apis/ql"; // provides TypeScript code completion on SELECT,
                                           // but cds-ts fails to run

const cds = require("@sap/cds");           // cds-ts runs, but TypeScript code completion is
const { SELECT } = cds.ql;                 // lost.

Thanks!

View Entire Topic

Hi John,

there is no need to require ql statements from cds, as they are globally defined, so code like this should work in TS without importing anything:

const sel = SELECT.from(Foos).columns("x")
sel.from(Foo)
sel.columns("x")<br>
I will still look into the issue, that the code completion is missing, when using cds.ql.SELECT as type.

Btw this also works for me:

import * as cds from "@sap/cds"; 
const { SELECT } = cds.ql
const sel = SELECT.from("Foo")