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
nodeinstead ofdenowill fail without extra setup. - Not using the
.tsextension 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
.tsfiles for TypeScript code. - Run with
deno run filename.ts. - Add permissions like
--allow-readif 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.