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 liketarget(JavaScript version),module(module system), andstrict(type checking rules).includeandexclude: 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.jsonin 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.jsonin 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:
| Option | Description | Example Value |
|---|---|---|
| target | JavaScript version to compile to | "es2020" |
| module | Module system used | "esnext" |
| strict | Enable strict type checking | true |
| jsx | JSX code generation mode | "react" |
| lib | List of library files to include | ["dom", "esnext"] |
| include | Files/folders to include | ["./src/**/*"] |
| exclude | Files/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.