0
0
DenoHow-ToBeginner ยท 3 min read

How to Configure TypeScript in Deno: Simple Setup Guide

Deno uses TypeScript out of the box without extra setup, but you can customize it by creating a tsconfig.json file in your project root. This file lets you control compiler options like target version and module resolution. Run your code with deno run and Deno will automatically apply your TypeScript configuration.
๐Ÿ“

Syntax

The main way to configure TypeScript in Deno is by creating a tsconfig.json file. This JSON file contains compiler options that tell Deno how to handle your TypeScript code.

Key parts include:

  • compilerOptions: Settings like target (JavaScript version), module (module system), and strict (type checking rules).
  • include and exclude: Which files or folders to compile.

Deno automatically looks for tsconfig.json in the current directory when running scripts.

json
{
  "compilerOptions": {
    "target": "esnext",
    "module": "esnext",
    "strict": true,
    "jsx": "react",
    "lib": ["dom", "esnext"]
  },
  "include": ["./src/**/*"],
  "exclude": ["./tests/**/*"]
}
๐Ÿ’ป

Example

This example shows a simple tsconfig.json and a TypeScript file. Deno will use the config to compile and run the code.

typescript
// tsconfig.json
{
  "compilerOptions": {
    "target": "es2020",
    "module": "esnext",
    "strict": true
  }
}

// hello.ts
const greet = (name: string): string => {
  return `Hello, ${name}!`;
};

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

Common Pitfalls

Some common mistakes when configuring TypeScript in Deno include:

  • Not placing tsconfig.json in the project root, so Deno can't find it.
  • Using incompatible compiler options that Deno does not support.
  • Forgetting to restart the Deno process after changing tsconfig.json.
  • Expecting Deno to use tsconfig.json in subfolders automatically (it only uses the nearest one).

Always validate your JSON syntax and keep your config simple.

text
/* Wrong: tsconfig.json in a subfolder not detected by Deno */
// Project structure:
// /src/tsconfig.json
// /hello.ts

/* Right: Place tsconfig.json in project root */
// /tsconfig.json
// /hello.ts
๐Ÿ“Š

Quick Reference

Here is a quick summary of useful tsconfig.json options for Deno:

OptionDescriptionExample Value
targetJavaScript version to compile to"es2020"
moduleModule system used"esnext"
strictEnable strict type checkingtrue
jsxJSX code generation mode"react"
libList of library files to include["dom", "esnext"]
includeFiles/folders to include["./src/**/*"]
excludeFiles/folders to exclude["./tests/**/*"]
โœ…

Key Takeaways

Create a tsconfig.json file in your project root to customize TypeScript in Deno.
Deno automatically applies tsconfig.json when running scripts with deno run.
Keep compiler options compatible with Deno's supported features.
Place tsconfig.json at the root; subfolder configs are not automatically detected.
Restart Deno after changing tsconfig.json to apply new settings.