TypeScript · Middle · Question 40 of 75

How do you extend the type of the global Window object from a module?

export {};
declare global {
  interface Window { dataLayer: unknown[] }
}

Explanation

In a module file (with import/export), global declarations go inside declare global. The Window interface merges with your extension. Redeclaring type Window causes a duplicate identifier error.

Further reading

Problem with a question

TypeScript · Middle · question 40

Test yourself

Take the TypeScript · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz