Skip to content

Interface property not enforcing requirement for both getter and setter when implemented on a class #45517

Closed
@frank-weindel

Description

@frank-weindel

Bug Report

A property specified in an interface is not enforcing the requirement that both a getter and setter should exist when the interface is implemented on a class. See code example below...

🔎 Search Terms

interface getter setter accessor mutator property

🕗 Version & Regression Information

Seems to be an issue all the way from the nightly build back to 3.3.3 (as far as I can go back in the playground)

  • This is the behavior in every version I tried (in Playground), and I reviewed the FAQ for entries about interfaces, getters, and setters.

⏯ Playground Link

Playground link with relevant code

💻 Code

interface IMyInterface {
  property1: string; // Intuitively this should imply, when implemented, that it is both settable and gettable, regardless of the means
}

class MyClassWithSetterOnly implements IMyInterface { // <--- Expect error, because property1 is missing a getter (Callout A)
  private _property1: string = 'default';
  set property1(v: string) {
    this._property1 = v;
  }
}

class MyClassWithGetterOnly implements IMyInterface { // <--- Expect error, because property1 is missing a setter (Callout B)
  private _property1: string = 'default';
  get property1(): string {
    return this._property1;
  }
}

🙁 Actual behavior

  • The implements clause on Callout A's line above does not produce an error
  • The implements clause on Callout B's line above does not produce an error

🙂 Expected behavior

  • The implements clause on Callout A's line should produce an error similar to:
Class 'MyClassWithSetterOnly' incorrectly implements interface 'IMyInterface'.
  Property 'property1' is missing in type 'MyClassWithSetterOnly' but required in type 'IMyInterface'.ts(2420)
  • The implements clause on Callout A's line should produce an error similar to:
Class 'MyClassWithGetterOnly' incorrectly implements interface 'IMyInterface'.
  Property 'property1' is missing in type 'MyClassWithGetterOnly' but required in type 'IMyInterface'.ts(2420)

Understanding that missing half of a getter/setter set might produce a slightly different error for easier debugging and fix automation.

Alternative expectation

An alternative expectation may be for TypeScript to not allow a setter/getter to exist alone in any context (class, interface, object literal, etc). However, I imagine this would be very troubling for developers.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions