A.K
A.K

Reputation: 403

Failed to compile - property does not exist on type Window

Augmenting window object fails to compile with typescript 3.5.3.

Before upgrading typescript, it had worked.

// somescript.ts
declare global {
  /* tslint:disable */
  type Window = {
    _TEST_PROP?: any
  }
  /* tslint:enable */
}

//someOtherScript.ts
window._TEST_PROP = {
 foo: 'bar'
}

Error: Property '_TEST_PROP' does not exist on type 'Window'.

I expect it not to fail compilation, as it had already worked before

Upvotes: 0

Views: 302

Answers (1)

Titian Cernicova-Dragomir
Titian Cernicova-Dragomir

Reputation: 250366

Not sure if this ever worked without errors, i expect it did not. Type aliases don't merge, interfaces do. Window is an interface, so you can augment it.

// somescript.ts
declare global {
  interface Window {
    _TEST_PROP?: any
  }
}

//someOtherScript.ts
window._TEST_PROP = {
 foo: 'bar'
}

Upvotes: 1

Related Questions