5.2 Basic hello world

Typescript code

Let's begin by exploring the most straightforward hello world program, written using Typescript. This program could also be referred to as a console print program, as it outputs information to the console. Throughout this discussion, we'll use the terms "hello world" and "console print" interchangeably.

There are two key motivations for utilizing Typescript over Javascript within the context of Deno:

  1. Deno inherently supports Typescript, making it the preferred choice for developing user-level programs. While Deno does provide support for Javascript, the core essence of using Deno could be undermined if Typescript isn't embraced.

  2. Creating a program in Typescript allows us to observe the process of converting it into Javascript. It's important to recall that the V8 engine, which powers Deno, can only execute Javascript code.

Now, let's delve into the most basic console print program written in Typescript. It's worth noting that this slightly differs from what one might anticipate as a typical console log program. The purpose behind this variation is to highlight certain aspects of Typescript and to showcase the transformation process it undergoes to become Javascript. This exploration provides valuable insight into how Deno operates and manages different code languages.

The file name is helloLog.ts.

The location of the source file is:

/Users/mayankc/Work/source/denoExamples/helloLog.ts

The purpose of indicating the path of the source file is to demonstrate how it is retrieved during the compilation of the program. This provides insight into the process of fetching the necessary files. Let's take a look at the content present within the "helloLog.ts" source file:

// File helloLog.ts

function printNumber(input: number) {
  console.log(input);
}

function printString(input: string) {
  console.log(input);
}

printNumber(1);
printString("One");

The program is quite straightforward. It consists of just two functions: one for displaying numbers and the other for showing strings. It might not seem like much, but these functions are enough to understand the very basics of Deno.

Javascript code

As we discussed earlier, Deno comes with built-in support for Typescript. However, it's important to note that the V8 engine, which powers Deno, is designed to execute Javascript code exclusively. This means that before code can run in V8, any Typescript code must be transformed into Javascript. Deno takes on the task of converting Typescript to Javascript prior to loading and running it within the V8 engine.

To achieve this conversion, Deno relies on a Rust-based tool called the SWC compiler. This compiler is responsible for translating Typescript code into its Javascript counterpart, ensuring that it becomes compatible with the V8 engine's execution environment. This translation process is a pivotal step in the Deno ecosystem, enabling developers to seamlessly work with Typescript while still benefiting from the performance and capabilities of the V8 engine.

Additionally, when there's a need to perform type-checking during the startup phase, Deno leverages Microsoft's TSC compiler. This step is crucial in ensuring that the Typescript code adheres to the specified type rules and structures, helping catch potential errors early in the development process. By incorporating TSC into its workflow, Deno provides developers with a comprehensive solution for managing Typescript code from conversion to type validation.

The following is the output of the converted input file:

// File helloLog.ts
function printNumber(input) {
    console.log(input);
}
function printString(input) {
    console.log(input);
}
printNumber(1);
printString("One");
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImZpbGU6Ly8vVXNlcnMvbWF5YW5rYy9Xb3JrL3NvdXJjZS9kZW5vRXhhbXBsZXMvaGVsbG9Mb2cudHMiXSwic291cmNlc0NvbnRlbnQiOlsiLy8gRmlsZSBoZWxsb0xvZy50c1xuXG5mdW5jdGlvbiBwcmludE51bWJlcihpbnB1dDogbnVtYmVyKSB7XG4gIGNvbnNvbGUubG9nKGlucHV0KTtcbn1cblxuZnVuY3Rpb24gcHJpbnRTdHJpbmcoaW5wdXQ6IHN0cmluZykge1xuICBjb25zb2xlLmxvZyhpbnB1dCk7XG59XG5cbnByaW50TnVtYmVyKDEpO1xucHJpbnRTdHJpbmcoXCJPbmVcIik7XG4iXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsbUJBQW1CO0FBRW5CLFNBQVMsWUFBWSxLQUFhO0lBQ2hDLFFBQVEsSUFBSTtBQUNkO0FBRUEsU0FBUyxZQUFZLEtBQWE7SUFDaEMsUUFBUSxJQUFJO0FBQ2Q7QUFFQSxZQUFZO0FBQ1osWUFBWSJ9

The process of converting the code was relatively straightforward. During compilation, the argument types were checked and aligned, and afterward, all the specific type details were eliminated to create clean JavaScript code.

It's worth mentioning that there's a comment located at the end regarding the "sourceMappingURL." This comment serves Deno's internal purposes, aiding in its functioning.

What adds to the simplicity of this program is the absence of imports or any other operations. The topics of imports and operations will be delved into more deeply in the upcoming chapter.

Although the program itself is quite basic, it serves as a valuable tool for comprehending the fundamental concepts. Let's run it once:

> deno run helloLog.ts 
1
One

It's time to embark on our journey by exploring the central program of Deno. This will provide us with a solid foundation to build upon as we progress.

Last updated