TypeScript-DOM-lib-generator icon indicating copy to clipboard operation
TypeScript-DOM-lib-generator copied to clipboard

fix: Mark `prototype` property of constructors as `readonly`

Open ExE-Boss opened this issue 4 years ago • 8 comments

This more closely matches runtime behaviour.


It also causes code that would try to re‑assign the prototype property of WebIDL constructors to have the error caught at compile time, rather than at run time.

ExE-Boss avatar Jan 03 '21 06:01 ExE-Boss

We think this is probably a bit too big of a change for its value, I agree that it better reflects reality (example below) but it's hard to gauge a potential breaks from something like this.

AbortController.prototype = () => {}
> () => {}

new AbortController()
> AbortController {signal: AbortSignal}

const a = new AbortController()
undefined

a
>  AbortController { 
  signal: AbortSignal {aborted: false, onabort: null} 
  __proto__: AbortController
}

orta avatar Feb 23 '21 16:02 orta

We think this is probably a bit too big of a change for its value, I agree that it better reflects reality (example below) but it's hard to gauge a potential breaks from something like this.

I'm not sure I understand the reasoning here, your example looks like it will benefit from readonly? The reason it's silently ignoring reassignation of .prototype is because it's not in strict mode.

"use strict"; AbortController.prototype = () => {}
> Uncaught TypeError: "prototype" is read-only

saschanaz avatar Dec 04 '21 13:12 saschanaz

(But I guess some polyfill can legally assign things to .prototype)

saschanaz avatar Dec 04 '21 13:12 saschanaz