0
0
DenoHow-ToBeginner ยท 3 min read

How to Use TypeScript in Deno: Simple Guide

Deno runs TypeScript files natively without extra setup by simply running deno run yourfile.ts. You write TypeScript code as usual, and Deno compiles and executes it automatically.
๐Ÿ“

Syntax

In Deno, you write TypeScript code in files with the .ts extension. You use standard TypeScript syntax including types, interfaces, and modern JavaScript features.

To run the code, use the command deno run filename.ts. Deno handles compiling and running the TypeScript file in one step.

typescript
function greet(name: string): string {
  return `Hello, ${name}!`;
}

console.log(greet("Deno"));
๐Ÿ’ป

Example

This example shows a simple TypeScript program in Deno that defines a typed function and prints a greeting.

typescript
function greet(name: string): string {
  return `Hello, ${name}!`;
}

console.log(greet("Deno"));
Output
Hello, Deno!
โš ๏ธ

Common Pitfalls

  • Trying to run TypeScript files with node instead of deno will fail without extra setup.
  • Not using the .ts extension can cause Deno to not recognize the file as TypeScript.
  • For scripts that access files, network, or environment, you must add explicit --allow-* permissions when running.
none
/* Wrong: running with node */
// node script.ts

/* Right: running with deno */
// deno run script.ts
๐Ÿ“Š

Quick Reference

Summary tips for using TypeScript in Deno:

  • Use .ts files for TypeScript code.
  • Run with deno run filename.ts.
  • Add permissions like --allow-read if needed.
  • Deno supports modern TypeScript features out of the box.
โœ…

Key Takeaways

Deno runs TypeScript files directly with no extra compilation step.
Use the .ts extension and run files with 'deno run filename.ts'.
Add explicit permissions for file, network, or environment access.
Avoid running TypeScript files with Node.js without setup.
Deno supports modern TypeScript syntax and features natively.