Use TypeScript with the SAP Cloud Application Programming Model

Use TypeScript with the SAP Cloud Application Programming Model

When I started covering the SAP Cloud Application Model topic as a Developer Advocate, I had to start working with a programming language I had tried to avoid over the past years: JavaScript.
All my past development experience was around type-safe programming languages like Swift or GoLang. Type-safe means that the compiler does type-checking on your code. The compiler will validate types while compiling and throw an error if a type is assigned or used invalidly, like dividing a number by an array of Strings. Jumping into JavaScript made my head hurt quite a bit because it allowed me, at least most of the time, to do whatever I felt like doing. Only during runtime or within my unit tests did my code throw errors. Unfortunately, these errors also didn’t help me understand what was happening.

const obj = { width: 10, height: 15 };

const area = obj.width * obj.heigth;
// This will result in NaN.

You can see where JavaScript comes from and what it was initially intended for – writing small, self-contained code scripts to complete smaller, concise tasks like handling a button press. It was created to make websites dynamic and enhance the user experience by providing advanced, interactive features. Over time, more and more functionality was added to the language because developers started to write more extensive code to achieve complicated tasks within the browser. JavaScript also evolved into a language that allowed for code to run on the server side with the Node.js flavor. Having server-side software run with a language that provides no compile-time type-safety felt like it would make code harder to maintain, understand, and develop.

I appreciate where JavaScript comes from and see its benefits, like its speed, its ability to be used in many use cases and environments, and its power to give the developer a lot of freedom. However, I am more comfortable having a safety net below me, so I jumped onto a JavaScript flavor, adding a static type-checker during design time—TypeScript.

What is TypeScript?

TypeScript provides a static type-checker around the JavaScript core. It helps make code more readable by allowing the definition of types in code and adding the possibility to define object types through interfacing and compile-time type checking. The beauty is that TypeScript does not alternate the underlying JavaScript code. Drop your working JavaScript code one-to-one into a TypeScript file, and your code will run and compile. TypeScript is not altering the JavaScript core; it is adding type safety. When you compile and build your code, TypeScript will remove your type declarations so that the compiled binary only includes pure JavaScript. That means you get all the creative possibilities and power JavaScript provides, but it allows you to add a safety net when doing your acrobatic endeavors.

let number = 5
let string = “hello”

number = string
// The compiler will warn you that “string is not assignable to type the number.”

How do I get started?

Using TypeScript within your CAP projects is easy, and I will show you how!

Install the TypeScript npm package for NodeJS.npm i -g typescript ts-nodeCreate a tsconfig.json file. Within that file, you can define JavaScript runtime environment dependencies and compiler options for TypeScript. e.g.:{
“extends”: “@tsconfig/node12/tsconfig.json”,
“compilerOptions”: {
“preserveConstEnums”: true
},
“include”: [“src/**/*”],
“exclude”: [“**/*.spec.ts”]
}

Write your code in .ts files.

Use the cds-ts command instead of cds to invoke TypeScript instead of plain JavaScript.

The cds-ts command is being used to avoid precompiling TypeScript files to JavaScript on each execution; this will speed up the development process. For production, use cds to build the final binary!

CAP & TypeScript by example

If we use the Hello World example from the official CAP documentation, you would build a TypeScript CAP application like this:

Service Definition (world.cds):service say @(path: ‘/say’) {
function hello (to:String) returns String;
}TypeScript implementation (world.ts):import { Request } from “@sap/cds”

module.exports = class say {
hello(req: Request) {
return `Hello ${req.data.to} from a TypeScript file!`
}
}

The complete example can be found in the SAP-samples repository.

Conclusion

JavaScript is a powerful language widely used in web development, server-side programming, and many more places! It is an industry-standard, and millions of developers love it. However, it feels chaotic and gives way too much freedom to developers, so I decided to switch to TypeScript. TypeScript will provide the power of JavaScript but it’ll help me better understand the code written and ensure that I don’t do something silly. It is an excellent addition to programming with Node.js and CAP daily. 

I hope you enjoyed this little blog post about TypeScript, and I hope you will give it a try!

With that, keep Coding!

 

​ Use TypeScript with the SAP Cloud Application Programming ModelWhen I started covering the SAP Cloud Application Model topic as a Developer Advocate, I had to start working with a programming language I had tried to avoid over the past years: JavaScript.All my past development experience was around type-safe programming languages like Swift or GoLang. Type-safe means that the compiler does type-checking on your code. The compiler will validate types while compiling and throw an error if a type is assigned or used invalidly, like dividing a number by an array of Strings. Jumping into JavaScript made my head hurt quite a bit because it allowed me, at least most of the time, to do whatever I felt like doing. Only during runtime or within my unit tests did my code throw errors. Unfortunately, these errors also didn’t help me understand what was happening.const obj = { width: 10, height: 15 };const area = obj.width * obj.heigth;// This will result in NaN.You can see where JavaScript comes from and what it was initially intended for – writing small, self-contained code scripts to complete smaller, concise tasks like handling a button press. It was created to make websites dynamic and enhance the user experience by providing advanced, interactive features. Over time, more and more functionality was added to the language because developers started to write more extensive code to achieve complicated tasks within the browser. JavaScript also evolved into a language that allowed for code to run on the server side with the Node.js flavor. Having server-side software run with a language that provides no compile-time type-safety felt like it would make code harder to maintain, understand, and develop.I appreciate where JavaScript comes from and see its benefits, like its speed, its ability to be used in many use cases and environments, and its power to give the developer a lot of freedom. However, I am more comfortable having a safety net below me, so I jumped onto a JavaScript flavor, adding a static type-checker during design time—TypeScript.What is TypeScript?TypeScript provides a static type-checker around the JavaScript core. It helps make code more readable by allowing the definition of types in code and adding the possibility to define object types through interfacing and compile-time type checking. The beauty is that TypeScript does not alternate the underlying JavaScript code. Drop your working JavaScript code one-to-one into a TypeScript file, and your code will run and compile. TypeScript is not altering the JavaScript core; it is adding type safety. When you compile and build your code, TypeScript will remove your type declarations so that the compiled binary only includes pure JavaScript. That means you get all the creative possibilities and power JavaScript provides, but it allows you to add a safety net when doing your acrobatic endeavors.let number = 5
let string = “hello”

number = string
// The compiler will warn you that “string is not assignable to type the number.”How do I get started?Using TypeScript within your CAP projects is easy, and I will show you how!Install the TypeScript npm package for NodeJS.npm i -g typescript ts-nodeCreate a tsconfig.json file. Within that file, you can define JavaScript runtime environment dependencies and compiler options for TypeScript. e.g.:{
“extends”: “@tsconfig/node12/tsconfig.json”,
“compilerOptions”: {
“preserveConstEnums”: true
},
“include”: [“src/**/*”],
“exclude”: [“**/*.spec.ts”]
}Write your code in .ts files.Use the cds-ts command instead of cds to invoke TypeScript instead of plain JavaScript.The cds-ts command is being used to avoid precompiling TypeScript files to JavaScript on each execution; this will speed up the development process. For production, use cds to build the final binary!CAP & TypeScript by exampleIf we use the Hello World example from the official CAP documentation, you would build a TypeScript CAP application like this:Service Definition (world.cds):service say @(path: ‘/say’) {
function hello (to:String) returns String;
}TypeScript implementation (world.ts):import { Request } from “@sap/cds”

module.exports = class say {
hello(req: Request) {
return `Hello ${req.data.to} from a TypeScript file!`
}
}The complete example can be found in the SAP-samples repository.ConclusionJavaScript is a powerful language widely used in web development, server-side programming, and many more places! It is an industry-standard, and millions of developers love it. However, it feels chaotic and gives way too much freedom to developers, so I decided to switch to TypeScript. TypeScript will provide the power of JavaScript but it’ll help me better understand the code written and ensure that I don’t do something silly. It is an excellent addition to programming with Node.js and CAP daily. I hope you enjoyed this little blog post about TypeScript, and I hope you will give it a try!With that, keep Coding!   Read More Application Development Blog Posts articles 

#SAP

You May Also Like

More From Author

+ There are no comments

Add yours