Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Mark prototype property of constructors as readonly #968

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

ExE-Boss
Copy link
Contributor

@ExE-Boss ExE-Boss commented Jan 3, 2021

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 ExE-Boss requested a review from sandersn as a code owner January 3, 2021 06:39
@orta
Copy link
Contributor

orta commented Feb 23, 2021

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
}

@saschanaz
Copy link
Contributor

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
Copy link
Contributor

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

@petamoriken
Copy link
Contributor

Can we move this PR forward?

@saschanaz
Copy link
Contributor

We nowadays have better testing and hopefully the CI in microsoft/TypeScript side repo can catch potential breakage @orta was talking about on DefinitelyTyped. PR welcome (assuming one would also try the CI on TS repo too)

@jakebailey
Copy link
Member

jakebailey commented Aug 3, 2024

Before doing this, you probably need to consider microsoft/TypeScript#58296 as it may become necessary to remove the readonly modifier on some prototypes.

@saschanaz
Copy link
Contributor

Not sure what those some would be. Would that be because of compatibility issue with packages?

@jakebailey
Copy link
Member

No, it's because the PR would actually enforce that you can't assign an object with a readonly property to one that has the same property but mutable, and that relationship also applies to interfaces extending other interfaces. So, you can't inherit a mutable prototype and then mark it as readonly or vice versa.

But, maybe it would work out to mark all prototypes as readonly, it just doesn't seem like that's the way things are going based on the discussions about the PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants