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

Setting getter property should return error #51327

Closed
mspace opened this issue Oct 27, 2022 · 4 comments
Closed

Setting getter property should return error #51327

mspace opened this issue Oct 27, 2022 · 4 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@mspace
Copy link

mspace commented Oct 27, 2022

Bug Report

πŸ•— Version & Regression Information

4.6.4

⏯ Playground Link

Playground link with relevant code](https://www.typescriptlang.org/dev/bug-workbench/?ts=4.6.4#code/KYDwDg9gTgLgBAYwDYEMDOa4GEIFswpTABiAlksALIQAmwScA3gFDNxxgCuARkqQnADmweKRoAKAJQAuOGhhRSAO0FM27OERicoSuDAAWpNADoAZuWABJGiZgQAyguWCpJhBCUIUMcQHIAWj8AGn0jUyIAN2NSTzgAH3i4Pz9JAG51AF9Wdi5efjgLChtZJU5cbmAoOABeOAAGDNyePgEomM9ZeUUVWuS-DPU81sRPbs4EeyhxZVIYAH5ZAAVCGFIUJAAeHHxCEktqOiQAPkk1DTgAeW4AK2BJk3Q0UkElcUNjUNmYdKycjhaBWQnmAUlkOwIRDIFEO9HOGi0Oj0SmAAHdsHhIfsYbR6OIWBd2EVrDRZB9TMSbMF1BcxLI-Gg-DS4JlfuxstkgA))

πŸ’» Code

export class CompareFileModel {

  public get id(): string {
    return this.fileId.toString().concat('-', this.revision || '');
  }

  public fileId: number = 0;
  public revision: string = '';

  public constructor(init?: Partial<CompareFileModel>) {
    Object.assign(this, init);
  }

  public clone(): CompareFileModel {
    return new CompareFileModel({
      fileId: this.fileId,
      // id is getter property. It should not allow setting getter property
      id: 's'
    });
  }
}

Workbench Repro

πŸ™ Actual behavior

Typescript reported there is no error.

πŸ™‚ Expected behavior

Code should not compile, as we're setting getter property

@MartinJohns
Copy link
Contributor

MartinJohns commented Oct 27, 2022

It's a design limitation. TypeScript does not differentiate between properties with accessors and without.

This is essentially a duplicate of #9726.

@fatcerberus
Copy link

Yeah, duplicate of #9726, even though it doesn't look like it at first. The lack of a setter is irrelevant at the point you expect an error since you're just initializing an object literal, which wouldn't call it anyway. Even if there was a setter for id you'd probably still want to exclude that property from init if the setter did anything more complex than this._id = val.

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Oct 27, 2022
@RyanCavanaugh
Copy link
Member

Even if #9726 were implemented, mapped types like Partial would still include non-enumerable properties. Really you'd want an entirely new kind of distinction here, since I don't think the non-enumerability of id is the real crux -- if getters were enumerable tomorrow, the code would still be wrong. You'd want something like Settable<T> that was a mapped type that only produced settable properties, but we don't have write semantics anywhere in relatability.

@RyanCavanaugh RyanCavanaugh closed this as not planned Won't fix, can't repro, duplicate, stale Oct 27, 2022
@fatcerberus
Copy link

but we don't have write semantics anywhere in relatability.

So #21759, then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

4 participants