Last Updated On By Khizer Ali
Deno has recently released, and it is a new runtime for JavaScript and TypeScript. Deno is the enhanced version of Node.js, which you are already familiar with.
Ryan Dahl is the creator of Deno. He mentioned some shortcomings of Node.js and decided to come up with a new and better platform because it was nearly impossible to do those changes and enhancements in Node.js.
Table of Contents
TypeScript is a purely object-oriented programming language that was developed to work on the server-side. Syntactically,
TypeScript follows JavaScript with further features. That is why it is called as a superset of JavaScript.
Node.js was written in C++ and JavaScript, but Deno has built on Rust and JavaScript.
This is the most exciting thing about Deno because it supports both JavaScript and TypeScript, two of the languages that are growing fast nowadays. Yes, you read right, you can now code in TypeScript without configuring your environment to work in TypeScript because Deno does that automatically for you.
Since a large percentage of programmers and developers love to work in TypeScript, Deno gives you direct support for writing code in TypeScript. You can create your file with .ts extension, and you can run your TypeScript as well as JavaScript code in it.
Following is a simple piece of code that is written in JavaScript as well in TypeScript in a file app.ts.
This will produce the following output.
See, this is the beauty of Deno. You can code in any one of the languages according to your interest.
As we have already discussed that Deno supports both JavaScript and TypeScript as first-class languages at runtime.
So that’s mean that Deno requires a fully qualified module name with its extension. Because Deno has no “magical” Module resolution as we have in Node.js. So, we have to import modules as a file with its extension. We can directly import TypeScript modules.
For Example:
import { serve } from "https://deno.land/std/http/server.ts";
import { queue } from “./collections.ts”;
Deno supports three different ways for referencing type definition files without having to make use of “Magic” resolution.
If you are importing a JavaScript module, and you know the location where the type definition is located. Then you can specify the type definition at import. This will result in a Compiler hint.
// @deno-types="./app.d.ts"
import * as app from "./app.js";
If you are hosting a JavaScript module that you want to be used by Deno so, you need to inform Deno about the location of the type definitions.
In that case, you can utilize the triple-slash reference directive in the actual code.
For Example:
If you have a JavaScript module app.js which you want to import in Deno and you would like to provide the type definition alongside, then your module will look like this.
/// <reference types="./app.d.ts" />
export const app = "app";
If you are hosting a JavaScript module that you want to be used by Deno so, you need to inform Deno about the location of the type definitions. You can use a custom HTTP header of X-TypeScript-Types to inform Deno about the file definition.
This works in the same way as the above method except that the Js file’s content doesn’t need to be changed, and the server can identify the location.
Deno will use the compiler hint to load the .d.ts files, but some .d.ts files can not be resolved because it contains some unsupported features. Specifically, those files which expect to load reference type definitions from other packages using module resolution logic.
We’ve discussed that Deno gives you direct support of TypeScript and is capable of compiling and running code in both languages, JavaScript and TypeScript. But it doesn’t mean you have to learn an entirely new language. If you are already familiar with JavaScript moving into TypeScript is absolutely breeze.