Write High Quality JavaScript Code for Oracle Application Express
Old Handwritten Book by Kiwihug}
It's funny how JavaScript's 20th birthday was not as spectacular as it's shorter-named bretheren. When I first learned JavaScript in the late 1990's, I wasn't too excited as I was about Java, for two reasons most web developers know about JavaScript: weakly typed and non-standard browser implementation. The latter has been addressed by ECMA, but does that mean our code can now be more predictable and consistent?
TypeScript attempts to solve these issues by introducing static typing. This paves the way for a more structured language that results in better tooling and developer support. Think code hints. More importantly though, with compile-time error checking, we can be better assured of how the application would behave at runtime. And because we compile TypeScript to JavaScript, there is an opportunity to allow developers to work with modern ECMA specifications, but yet having the ability to compile down to earlier flavours of JS. All that without needing (not saying you shouldn't) to know the intricacies of transpilers and build management tools.
TypeScript introduces the concept of a declaration file. These files are like header files in C and package specifications in PL/SQL. Declaration files are usually written in files with a .d.ts
file extension. They do not compile, instead, simply describe the functions and variables available in a JS library. They assist in TypeScript code compilation, and with IDE support, developer productivity is enhanced with features like IntelliSense and type-checking. In Code, type-checking is also available in JS with a simple comment at the top of the file: //@ts-check
.
An Oracle Application Express (APEX) developer is no stranger to tab-switching the APEX API documentation, browser (Page Designer) and I hope, a comparable desktop IDE. When writing the JavaScript bits of the APEX application, developers have a few choices. The best practice though, suggests that we consolidate code into JS files. This aids version control and testing. It's no easy feat though, ask any experience APEX developer. Fortunately, Vincent Morneau came to the rescue with some (APEX) Nitro to boost productivity.
Vincent changed the paradigm of coding JS in APEX. We could gradually move some of our screen time away from Page Designer, and use mainstream code editors like Visual Studio Code (Code), Atom and Sublime, when working on JS, CSS and PL/SQL code. Without any fanfare, I had suggested and implemented TypeScript support in APEX Nitro that has been available since version 4.0.0-beta. For me, the final piece of the toolchain, would be a TS declaration file for the APEX JS API. Since there aren't any out there, I thought, why not write one? And so I did.
I am very excited about what benefits this might bring to the community and so, going away from my comfort zone, I have decided to make my work public so that fellow APEX developers can have a feel of what it's like working with TypeScript and what benefits it brings. I won't claim to be an expert in TypeScript or the APEX JS API. If you do find errors, please let me know through the usual channels!
Since TypeScript 2.0, declaration files are managed under the DefinitelyTyped project and made available through npm. You can find a list of available declaration files under the organization @types, or from their Github repository. For now, I have published my work at https://github.com/fuzziebrain/orclapex-js with instructions on how to use it in your environment. Eventually though, I'd like to publish this under DefinitelyTyped repository, so keep in touch to be informed of changes.
Some day though, I hope that the APEX JS API would proudly wear the badge:
TypeScript definitions on DefinitelyTyped (http://definitelytyped.org)
Happy coding!