Skip to content

A class with only getters satisfies some type without checking for the possibility of changing these fields #58141

New issue

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

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

Already on GitHub? # to your account

Closed
Crinax opened this issue Apr 10, 2024 · 2 comments
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@Crinax
Copy link

Crinax commented Apr 10, 2024

πŸ”Ž Search Terms

getters, types, classes, classes with getters

πŸ•— Version & Regression Information

  • This is incorrect type inference
  • This is the behavior in every version I tried, I tried on Nightly, 5.4.4, 3.3.3, 4.3.5

⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.4.4#code/C4TwDgpgBAggCgJwPZgM5QLxQN4CgoFQCWAJgFxQB2ArgLYBGECA3LgL664DGANgIap0MHPkJcklVMATUuwJAgAUYBEQBufYNAQQ+JCTxBQVKVBXjI0AShwdRBAOYRgxEoqsUaDJiMJ+oOsDUCJRQwAAWRKgAdCZo0aSsfhx24pIuEOaIpphUEADusIrYrhQAjFBsVqy4EAkkuQDMzEA

πŸ’» Code

type AProps = {
    id: number;
}

class A {
    constructor(private readonly props: AProps) {}

    get id(): number {
        return this.props.id;
    }
}

const e: AProps = new A({ id: 1 });

e.id = 3;

πŸ™ Actual behavior

Fails on execute without typescript errors

πŸ™‚ Expected behavior

It is necessary to notify the user that the class type and the user-defined type of the function argument are incompatible with each other

Additional information about the issue

No response

@jcalz
Copy link
Contributor

jcalz commented Apr 10, 2024

Getters without setters result inreadonly properties, which by #13347 are mutually assignable with read-write properties. This is all working as designed and is one of the mostly unavoidable soundness holes in the language.

@andrewbranch andrewbranch added the Design Limitation Constraints of the existing architecture prevent this from being fixed label Apr 10, 2024
@typescript-bot
Copy link
Collaborator

This issue has been marked as "Design Limitation" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Apr 13, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed
Projects
None yet
Development

No branches or pull requests

4 participants