#typescript issue: app.locals properties no longer accessible in astro project

1 messages · Page 1 of 1 (latest)

sturdy beacon
#

I'm working with a TypeScript project in Astro where I've defined a custom namespace for Astro's App.Locals interface. My declarations look like this:

export interface CustomUser extends User {
  role: string;
  preferredModuleType: string;
}

declare namespace App {
  interface Locals {
    flashError: string;
    user: CustomUser | null;
    session: import("better-auth").Session | null;
  }
}

However, I've suddenly lost access to these properties (flashError, user, session) in my code where I previously could access them through locals. TypeScript no longer recognizes these properties as existing on the locals object. I haven't changed the type declarations, so I'm not sure what could be causing this. Has anyone encountered a similar issue with Astro's type system or can suggest what might be breaking my type definitions?

oblique river
#

Can you show the whole file? Standard imports usually break global augmentation

#

Instead you should use inline imports (not sure that's what they're called)

#

Eg.

-import type { X } from "./foo"
+type X = import("./foo").X
sturdy beacon
#

thank you for a shot. the real reason was export keyword in front of interface CustomUser

As soon as a .d.ts file contains any top-level import or export, TypeScript stops treating it as an ambient (global) declaration file and instead treats it as an internal “module”.
When that happens, everything you declared inside the file—​including your namespace App augmentation—​stays inside that module and is no longer visible to the rest of your project. This behaviour is a TypeScript rule, not an Astro quirk
GitHub
.

That’s why the compiler suddenly says that flashError, user, and session don’t exist on locals: the global merge that used to provide them is now hidden.

o3 fixed it

oblique river
#

Yeah I mentionned impots but that's also true for exports

#

Glad you figured it out!