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

SUGGESTION: more strict checks on write-only properties #46039

Closed
4 of 5 tasks
YunHsiao opened this issue Sep 24, 2021 · 4 comments
Closed
4 of 5 tasks

SUGGESTION: more strict checks on write-only properties #46039

YunHsiao opened this issue Sep 24, 2021 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@YunHsiao
Copy link

YunHsiao commented Sep 24, 2021

Suggestion

πŸ” Search Terms

write-only properties
strict type check

βœ… Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

Write-only properties have not been the most type-checked area of the language (#21759), but in some cases though we can almost always tell it's not what's intended and throw some error messages in there.

πŸ“ƒ Motivating Example

The example below (playground link here) demonstrates a peculiar case that could definitely benefit from some type error messages, but the suitable situations may go well beyond this simple case and are open for discussion.

class A {
    private _v = [0];

    set v0 (v: number) {
        this._v[0] = v;
    }

    get array (): Readonly<number[]> {
        return this._v;
    }
}

const x = new A();

x.v0 = 10; // correct usage
x.v0 += 10; // incorrect usage (results in NaNs), which should not pass type check!

πŸ’» Use Cases

@FilipeBeck
Copy link

It is also allowing const v0 = x.v0 without having accessor method

@andrewbranch
Copy link
Member

Duplicate of #21759 (and more directly #30852)β€”the problem is we don’t recognize this as write-only at all.

@andrewbranch andrewbranch added the Duplicate An existing issue was already created label Sep 30, 2021
@thw0rted
Copy link

@andrewbranch would you consider reopening this in light of the introduction of Variant Accessors, or at least include it if/when #43662 comes along? Should we just update that ticket to make sure that the write-only use case is considered?

@andrewbranch
Copy link
Member

Seems reasonable to discuss in #43662, and #21759 is still open as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants