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 using Typescript over Javascript within the context of Deno:
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 used.
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 look at the simplest console print program written in TypeScript. This program is slightly different from a typical console log program. We made this variation to show how TypeScript works and how it is converted to JavaScript. By examining this program, we can gain valuable insight into how Deno works with different programming 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:
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:
Converting the code was a simple process. During compilation, the argument types were checked and aligned, and then the specific type details were removed to create clean JavaScript code.
Note that there is a comment at the end about "sourceMappingURL." This comment is for Deno's internal use and helps it function. This program is simple because it doesn't have imports or other operations. We will discuss imports and operations in more detail in the next chapter.
Although this program is basic, it helps us understand the fundamental concepts. Let's run it now:
Now, let's start our journey by examining the main program of Deno. This will give us a solid foundation to build on as we move forward.
Last updated