Skip to content

Cannot set readonly property in IIFE #60413

Closed

Description

@medikoo

🔎 Search Terms

"readonly", "iife"

🕗 Version & Regression Information

  • This is the behavior in every version I tried

⏯ Playground Link

No response

💻 Code

class Foo {
	readonly bar: string = "";
	constructor() {
		(() => { this.bar = "test"; })();
	}
}

🙁 Actual behavior

Fails with Cannot assign to 'bar' because it is a read-only property. error

🙂 Expected behavior

Should work in exactly same way as:

class Foo {
	readonly bar: string = "";
	constructor() {
		this.bar = "test";
	}
}

Additional information about the issue

I assume that's expected for some reason, and I'd love to learn what are the logical grounds for that

Activity

  1. MartinJohns commented on Nov 4, 2024

    @MartinJohns
    Contributor

    Essentially another duplicate of #9998. #58729 would likely fix this. This was nonsense, as pointed out by jcalz.

  2. jcalz commented on Nov 4, 2024

    @jcalz
    Contributor

    Martin Johns (@MartinJohns) aren’t IIFEs already inlined for CFA? I’m guessing initialization of readonly fields must be handled separately.

    (Also, I don't see how #58729 would help; at the very least, merely running that same code in a TS with some version of that PR in it doesn't seem to change anything. Not sure where adding immediate/deferred would make sense there either.)

  3. MartinJohns commented on Nov 4, 2024

    @MartinJohns
    Contributor

    You're right, my mistake. I swear I saw this issue before, but can't find it. :-/

  4. locked as resolved and limited conversation to collaborators on Oct 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptHelp WantedYou can do this

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions