#How do I extend the window type with my own global variables in Astro w/ TypeScript?

7 messages · Page 1 of 1 (latest)

verbal shuttle
#

I have some extensions on window that I wish to use across my <script> tags for managing some menu states. The logic works fine with the dev server but there are type errors. Specifically Property 'scrollHiding' does not exist on type 'Window & typeof globalThis'.ts(2339)

I've tried creating a globals.d.ts file containing

declare global {
    interface Window {
        scrollHiding: {
            header: Boolean,
            footer: Boolean,
        }
    }
}

but this type definition isn't being picked up by TypeScript. I've also tried moving it into a src/types directory and listing it as a "typeRoots" in tsconfig/json. What do I have to do to get this typechecking?

thorny grotto
#

I would also like to know the answer to this question.

verbal shuttle
#

I've settled for // @ts-ignore for now but I'd love to know the working solution.

subtle lily
#

You can add the following to your env.d.ts:

interface Window {
    customThing: string
}
#

Should work

#

(at least, it works for me right now)