Skip to content

Document how to extend window #2662

@Princesseuh

Description

@Princesseuh

Sometimes, you want to add a property to the window interface. TypeScript doesn't really document how to do this properly, and this situation is more likely to arise in Astro since we support TypeScript directly in script tags.

The solution is to add to env.d.ts (or any other .d.ts that's in the project scope) a Window interface, like such:

interface Window {
  helloChris: string;
}

The property will then appear in completions and by type-checked with no issues:

image

Side note

A similar situation happens for globalThis, but the way to add property to it is completely different, as you need to do the following:

export {}
declare global {
  function myFunction(): boolean;
  var myVariable: number;
}

image

Metadata

Metadata

Assignees

No one assigned

    Labels

    add new contentDocument something that is not in docs. May require testing, confirmation, or affect other pages.good first issueGood for newcomers

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions