Skip to content
Tutorials Center
My learning

Topics

Lesson 5 of 37 ยท Language and Runtime Basics

TypeScript in Deno

Use TypeScript in Deno with no setup. Learn how running differs from type checking, how deno check works, strict compiler options in deno.json, type-only imports and JSX.

TypeScript is the main language of Deno. Every Deno file can be .ts, .tsx, .js, .jsx, .mjs or .mts, and you never install a compiler. But there is one point that surprises most learners: running a file and type checking it are separate steps. This lesson explains how they relate, how to configure TypeScript in deno.json, and which TypeScript features you can use.

If you are new to TypeScript itself, you can follow along anyway: the examples are small, and the runnable ones are plain TypeScript that works on any runtime.

Running is not checking

When you type deno run main.ts, Deno removes the type annotations and runs what is left. It does not stop on type errors by default. That keeps startup fast, since type checking a large project can take seconds.

To check types, ask for it explicitly:

Terminal
deno check main.ts          # check one entry point and its imports
deno check **/*.ts          # check many files
deno run --check main.ts    # check, then run

Consider this file:

TypeScript
function double(n: number): number {
  return n * 2;
}

console.log(double("21"));

deno run prints 42, because JavaScript coerces the string. deno check reports:

TEXT
error: TS2345 [ERROR]: Argument of type 'string' is not assignable to parameter of type 'number'.

A good habit is to run deno check in your editor (the language server does it live), in your check task and in CI. deno test also type checks the test modules.

flowchart LR
  A["main.ts"] --> B{"deno run"}
  A --> C{"deno check"}
  B --> D["Strip types and execute"]
  C --> E["Full type analysis"]
  E --> F["Errors reported, nothing runs"]

The same source goes through two different paths.

Strict by default

Deno enables TypeScript's strict mode out of the box. That turns on strictNullChecks, noImplicitAny, strictFunctionTypes and the other strict flags. New Deno code therefore gets the safest settings without a tsconfig.json.

You can still adjust options in deno.json under compilerOptions:

JSON
{
  "compilerOptions": {
    "strict": true,
    "noUnusedLocals": true,
    "noImplicitOverride": true,
    "noUncheckedIndexedAccess": true,
    "jsx": "react-jsx",
    "jsxImportSource": "preact"
  }
}

Only a supported subset of TypeScript's options can be changed, because Deno controls the module system, the target and the emit. You do not set module, outDir or target.

Types you will write daily

The language itself is plain TypeScript. This example uses interfaces, generics and narrowing. It runs on the site's TypeScript runner, and the same file runs under deno run.

TypeScript Interfaces, generics and narrowing

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

Union types and exhaustive checks

TypeScript is at its best when you model states precisely. A discriminated union gives every shape a literal tag, and the compiler makes sure you handle each one.

TypeScript Discriminated unions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

Type-only imports

Because Deno removes types one file at a time, it must know which imports are only types. Use import type (or the inline type keyword) for anything that exists only in the type system:

TypeScript
import type { User } from "./models.ts";
import { createUser, type Role } from "./models.ts";

Deno enables TypeScript's verbatimModuleSyntax behaviour, so a missing type on a type-only import is an error. That rule is a feature: it keeps the emitted JavaScript predictable, and it matches how Node's type stripping works too.

Features that need care

Deno supports essentially all TypeScript syntax, including enums, namespaces and decorators. A few notes:

TypeScript
enum Color { Red, Green } // supported by Deno

Some runtimes that only strip types cannot run enums, parameter properties or namespaces, because those need code generation. If you plan to share code between Deno and such runtimes, prefer union types of string literals over enums:

TypeScript
type Color = "red" | "green";

Decorators follow the current ECMAScript proposal. experimentalDecorators is available through compilerOptions if a library needs the older form.

JavaScript with types

You can get type checking in plain .js files too. Add a comment at the top and Deno's language server checks the file using JSDoc annotations:

JavaScript
// @ts-check

/**
 * @param {number} a
 * @param {number} b
 * @returns {number}
 */
export function add(a, b) {
  return a + b;
}

To use types for a JavaScript module that has none, point to a declaration file with a comment on the import:

TypeScript
// @ts-types="./lib.d.ts"
import { greet } from "./lib.js";

JSX and TSX

.tsx files work with no build step. Choose how JSX is turned into calls with compilerOptions. For React 19 or Preact:

JSON
{
  "compilerOptions": {
    "jsx": "react-jsx",
    "jsxImportSource": "preact"
  },
  "imports": {
    "preact": "npm:preact@10"
  }
}

Suppressing errors honestly

Occasionally you must tell the compiler to trust you. Prefer @ts-expect-error over @ts-ignore, because it turns into an error if the problem is later fixed, so stale comments do not pile up:

TypeScript
// @ts-expect-error legacy API returns a string here
const length: number = oldApi();

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Does deno run main.ts stop when there is a type error?

    deno run only strips types. Type checking is a separate step: deno check or the --check flag.
  2. 2. Where do you configure TypeScript options in a Deno project?

    deno.json has a compilerOptions field for the supported subset of options.
  3. 3. Which import style should you use for a type that exists only in TypeScript?

    Type-only imports are marked so Deno can erase them without analysing other files.
  4. 4. What does the never assignment in a switch default branch give you?

    If a new union member is not handled, shape is no longer never and the assignment fails to compile.

Summary

  • deno run strips types and runs; deno check (or --check) analyses them.
  • TypeScript is strict by default; tune supported options in compilerOptions inside deno.json.
  • Use import type for type-only imports.
  • Enums and decorators work, but unions of literals travel better across runtimes.
  • // @ts-check and JSDoc give type checking to plain JavaScript.
  • JSX works without a build step when jsx options are set.

Next: The Deno Namespace and Runtime APIs