5.2 Basic hello world
We'll start with the simplest possible hello world program which is written in Typescript. This program could also be called a console print program as the program prints out on the console. We'll use hello world or console print interchangeably.
There are two reasons to use Typescript rather than Javascript:
- Deno supports TS out of the box, so it's user-level programs are mostly going to be in Typescript. It's not that JS is not supported, but the primary driver to use Deno would get lost if people don't use TS with Deno
- Having a program Typescript enables us to see how the conversion to Javascript happens (recall that V8 can only run JS code)
Here is the simplest console print program in typescript. Note that this is a bit different from what someone would expect from a simple console log program. The reason is to show some elements of Typescript, and how it gets converted to Javascript.
The file name is
helloLog.ts
.The location of the source file is:
/Users/mayankc/Work/source/deno-vs-nodejs/helloLog.ts
The reason to mention the path of the source file is to show how it gets fetched when the program compiles.
Here is the source inside helloLog.ts:
// File helloLog.ts
function printNumber(input: number) {
console.log(input);
}
function printString(input: string) {
console.log(input);
}
printNumber(1);
printString('One');
The program is very simple. There are two functions: one to print a number, the other one to print a string. That's all.
As mentioned above, although Deno supports Typescript out of the box, the V8 engine can only run Javascript code. Deno has to convert all the Typescript code to Javascript before it loads and executes in v8. Deno uses Rust based SWC compiler for Typescript to Javascript conversion. Deno also uses Microsoft's TSC compiler if type-checking is required at startup.
Here is the converted output:
"use strict";
function printNumber(input) {
console.log(input);
}
function printString(input) {
console.log(input);
}
printNumber(1);
printString('One');
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiZmlsZTovLy9Vc2Vycy9tYXlhbmtjL1dvcmsvc291cmNlL2Rlbm8tdnMtbm9kZWpzL2hlbGxvTG9nLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7QUFBQSxTQUFTLFdBQVcsQ0FBQyxLQUFhO0lBQzlCLE9BQU8sQ0FBQyxHQUFHLENBQUMsS0FBSyxDQUFDLENBQUM7QUFDdkIsQ0FBQztBQUVELFNBQVMsV0FBVyxDQUFDLEtBQWE7SUFDOUIsT0FBTyxDQUFDLEdBQUcsQ0FBQyxLQUFLLENBQUMsQ0FBQztBQUN2QixDQUFDO0FBRUQsV0FBVyxDQUFDLENBQUMsQ0FBQyxDQUFDO0FBQ2YsV0FBVyxDQUFDLEtBQUssQ0FBQyxDQUFDIn0=
This conversion was quite easy. The argument types were matched at compile time and then all the type information was removed to make it pure JS code.
Note that there is a comment at the end
sourceMappingURL
. This is for Deno's internal use. Another thing that makes this program simple enough is that there are no imports or any other ops. Import and ops are the focus of the next chapter.
The program is very simple, but it'd be very helpful in explaining the core concepts. Let's start our journey with the main program of Deno.