In this example, Our goal is to setup and create CAP Application for TypeScript.
We will be following below steps-
- 1. Creating a CAP App.
- 2. Install the Dependencies.
- 3. Initializes TypeScript in your project.
- 4. Create custom type declaration for @sap/cds.
- 5. Update tsconfig.json File.
- 6. Update Script on `package.json` file.
- 7. Create Service cds file.
- 8. Create Custom Service Implementation ts file.
- 9. How to run the project.
Step 1 - Creates a new CAP project
Use the below command to create a CAP Application -
Here, our Application name is tscap
Now, go inside the Project Folder, and install the dependencies. -
Step 2 - Install the Dependencies
Use the below commands to install the dependencies to setup the project.
ORBelow are the details of the Dependencies we are installing -
typescript - The TypeScript compiler (tsc). It lets you write code in .ts files and compile it to JavaScript.
ts-node - Runs TypeScript files directly without compiling them first — useful for quick scripts or development.
tsx - A fast, modern TypeScript/ESM runner similar to ts-node, but faster and supports modern module systems (ESM, JSX, etc.). Many new projects prefer tsx.
@types/node - TypeScript type definitions for Node.js built-in modules (like fs, path, etc.). Without this, TypeScript wouldn’t know Node’s API types.
@sap/cds - The SAP Cloud Application Programming Model (CAP) core library. It provides tools and APIs to define CDS models (.cds files), services, and entities for SAP applications.
What happens when you run it -
1. npm downloads all these packages and their dependencies.
2. Adds them to your package.json under devDependencies.
3. Creates or updates a package-lock.json file.
4. Installs them into your node_modules directory.
To install sqlite3, we can use below command -
Step 3 - Initializes TypeScript in your project
It Initializes the typescript in the project and creates tsconfig.json file.
Step 4 - Create custom type declaration for @sap/cds
Create a file named - types/@sap__cds/index.d.ts
Inside the file, declare a module and define cds of type any as below -
Step 5 - Update tsconfig.json File
On the tsconfig.json file, update the below properties -
target : ES2020
module : CommonJS
rootDir : srv
outDir : gen/srv
esModuleInterop : true
Also define the typeRoots and include.
You can copy the below details and replace the content of tsconfig.json file
Step 6 - Update Script on `package.json` file
In the package.json file add the below script in the script section.
Step 7 - Create Service cds file
Create your service cds file (lets say MyService.cds file) on srv folder and define the service like below-
Step 8 - Create Custom Service Implementation ts file
Create your custom service implementation ts file (lets say MyService.ts file) on srv folder and define the service like below-
Step 9 - How to run the project
Use the below command to run the project -
And it is done, You can deploy your app and use it. !!!