AI Workshop: learn to build apps with AI →
TypeScript: How I fixed some trouble importing types in .d.ts files

Join the AI Workshop and learn to build real-world apps with AI. A hands-on, practical program to level up your skills.


I had some trouble making something work in my Astro site.

I used Astro locals and I had to type a variable I shared using locals.

So I went and added that to the src/env.d.ts, as the docs say.

But my types weren’t picked up.

My code looked like this:

/// <reference types="astro/client" />
import { sometype } from 'somelib'

declare namespace App {
  interface Locals {
    somevariable: sometype
  }
}

What I discovered thanks to this SO answer is, we can’t do imports like this in .d.ts files.

So I imported the type in another file, and then I imported the type from that file, like this:

typesforenvdts.ts

import { sometype } from 'somelib'

export { sometype }

env.d.ts

/// <reference types="astro/client" />

declare namespace App {
  interface Locals {
    somevariable: import('./typesforenvdts').sometype
  }
}

Don’t ask me why, but now it works.

Lessons in this unit:

0: Introduction
1: Your first TypeScript program
2: Types
3: Typing functions
4: The editor helps you with type errors
5: Running TypeScript code
6: Valid types
7: Type aliases and interfaces
8: Union types
9: Typing arrays with generics
10: The DX of editing TypeScript
11: There's more...
12: TypeScript declare a type that can be a string or array of strings
13: TypeScript, disable checks for `declared but its value is never read`
14: Object destructuring with types in TypeScript
15: TypeScript Tutorial
16: Zod: Type-Safe Schema Validation for TypeScript
17: ▶︎ How I fixed some trouble importing types in .d.ts files