0

If I use interface and export class in same typescript file then I'm getting exception like follows.

Sample Code:

interface Window {
    Test: string;
}

window.Test = "cool";

export class Person {
    name: string;

    constructor(name: string) {
        this.name = name;
    }

    print(): void {
        console.log(this.name);
    }
}

Exception : Property 'Test' does not exist on type 'Window & typeof globalThis'.ts(2339)

if I remove class definition then error banishes. I googled for many hours but unable to find the reason. Could someone describe the reason behind this?

PS: I'm a newbie to Typescript and I'm exploring it to integrate it to my existing project. Pardon me, If my question is too amateurish.

3
  • Does this answer your question? TypeScript error: Property 'X' does not exist on type 'Window' Commented Jan 29, 2023 at 4:58
  • Not exactly. If I extend Window inside declare global parenthesis then error vanishes. Could you clarify on this ? Commented Jan 29, 2023 at 5:04
  • 1
    The declare global block is a way to tell TypeScript that you want to add a new property to the Window interface, even though it's not defined in the built-in interface. By adding declare global block in your code, you are telling TypeScript that the Test property should be considered part of the Window interface and it should allow you to add that property to the window object without raising an error. Commented Jan 29, 2023 at 6:19

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.