Final Note

Why are JS++ type guarantees necessary? Because, with unsafe JavaScript, even large websites with resources, like GoDaddy, can have broken checkouts resulting in lost sales from a single TypeError:

In the above, the “Continue” button never works so the shopping cart checkout cannot be finished.

Curiously, the checkout failure comes from the same ‘toLowerCase’ method that we explored in Chapter 9. Since JS++ is the only sound, gradually-typed programming language for JavaScript, type guarantees mean more than just adding “types” to JavaScript. It means the types are guaranteed to be correct when you declare them, and there are no edge cases that can result in runtime failures (unlike attempts to add data types by Microsoft and Facebook).

Don’t be the one to break critical code because someone told you to write the code in JavaScript instead. You need more than types and type checking, you need type guarantees.



JS++ | Type System

Similar Reads

RGBtoHex: Using JavaScript from JS++

We’re going to create two files, a JavaScript file and a JS++ file, to introduce how they interact together. We’re going to use the classic ‘rgbToHex’ example to understand JS++ type guarantees....

Type Guarantees

“Type guarantees” are a unique feature of JS++ that mean your types are guaranteed to be correct during compile-time analysis and runtime code execution even when you’re polluting your JS++ code with untyped JavaScript code. It’s the closest thing you’ll find to the type safety featured in safer languages that don’t have to deal with large collections of untyped code....

RGB Values

The RGB color model defines three color values: red, blue, and green. These color values are numeric and must fit within the range 0-255. JS++ actually happens to have a data type that exactly fits the specification of being numeric and guaranteeing numbers to be in the 0-255 range: the ‘byte’ data type....

Examining the Untyped JavaScript

RGB values must be within the 0 to 255 range. Since JavaScript lacks data types, we have to manually perform these checks. Let’s inspect how we handle this in our ‘rgbToHex’ JavaScript function....

Using JavaScript More Safely

You can use JavaScript more safely from JS++ than you can from JavaScript itself....

Move rgbToHex to JS++

First, let’s stop importing the JavaScript ‘rgbToHex’ function by removing the ‘external’ statement that imports it. Our main.jspp file should now just look like this:...

Converting the “Untyped” JavaScript into “Typed” JS++

We’ll start converting our JavaScript ‘rgbToHex’ function into JS++ by removing all the runtime checks and errors. You won’t be needing them anymore....

Add More Types

Our ‘rgbToHex’ function is still declared with the keyword ‘function’. This is the JavaScript way of declaring a function, and it still leaves our function unsafe....

Other Considerations

As we learned in the previous chapter about JavaScript, we intuitively have a sense of the data type to expect....

Final Note

Why are JS++ type guarantees necessary? Because, with unsafe JavaScript, even large websites with resources, like GoDaddy, can have broken checkouts resulting in lost sales from a single TypeError:...