Repository: TypeScript
Issue: Ability to type check setters in interface implementations
Labels: Suggestion,Awaiting More Feedback

### 🔍 Search Terms

setter interface extends typecheck

### ✅ Viability Checklist

- [X] This wouldn't be a breaking change in existing TypeScript/JavaScript code
- [X] This wouldn't change the runtime behavior of existing JavaScript code
- [X] This could be implemented without emitting different JS based on the types of the expressions
- [X] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- [X] This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- [X] This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals

### ⭐ Suggestion

Currently when implementing an interface, the type of the setter in the interface (when implicitly mutable or when given an explicit setter type) is not checked against the implementation. This means that an incorrect interface or an incorrect implementation can result in read-only or getter only properties getting assigned, and setters getting called with invalid typed.

I'm assuming this is working as intended due to the intentional not considering of readonly when computing assignability between types for legacy compatibility purposes.

This however doesn't prevent a strictness option from being added to allow projects to opt into having stricter checking of their interface implementations, which I think could be a compatible change.

I think this would align well with TypeScript's design goal #1: "Statically identify constructs that are likely to be errors.".


### 📃 Motivating Example

[Playground link](https://www.typescriptlang.org/play/?noUncheckedIndexedAccess=true&target=9&useUnknownInCatchVariables=true&exactOptionalPropertyTypes=true&noImplicitOverride=true&noPropertyAccessFromIndexSignature=true&ts=5.5.3#code/JYOwLgpgTgZghgYwgAgJIDED2nkG8CwAUAJAAeAXMiAK4C2ARtMgD7IDOYUoA5gNxEBfIkQQAbOGzbIsOYLQAOoiLQjgpGbHiLFuEMMlIAKAJSUaDJrmRQ91KCGQBWXkJIB6N8gByONnshQAISCwoQeyBJswNwgPMhgOHDIAG5wotQomDBoMhGiopgA7lKR0SAq4PE4AESk1QB0ROEAyhDKUpgAJp3xABZw+qUxcUmp6SgJEQ6gAfBIyMD6EKSQIJ1SCHAOaQWFyLSYNsiY8tADwJggHdP6YP2DRztFED2XfSiIYNRpyGKR9chWu1kKJgABrCb3FIXcRgKR3FB+WhbMAXBAdbLVZardYNESXDjIGCUDQ4AC8VAgexkJn4hCangAKr1gCVRGxEvkiiVJGU4hwuCBuPCcJg7NYIHBXiBRABPKh0RhQRqEGD1UjICnVOj1XXVOkM5AAURWqnWyE6mAgG3EcmkmmxZvUMgANBbrfJFihSRKAI7UYA2EbsfxMGCHBUWKAsdicEZre2imXy+RQTDJYCdYO6MABIkR8xKkoJkC+UPRgZ5UQqsCy07IADCvQgCDBmsTyEda2dmgA-PEoBlkJR4OyIHTwshkAA9XuhQ2obJJPy5pisiIIL47FNpjOdF5uxbwuuItq0EXIRi-TAKCCwuULGKHF7IQrNhz7gK0UBxYDZBELOA0BzCg65yIoyiqJAPQIIcNibnKKp-JIiYAEwLAoSgVHCOSaAQJA5gYJhmIq0BWDYXz2E4LjaCuRFjBklCYPQABWLZgMYeCuK4oQzMBiDegAQnA0b4ToehEaYkZKjGAo8HSxB0UYDEQCRUayXGQrGHSPFhJ4RrJKor6LL0nakIowCbiGq5QFIoDvIBswCYemGQeAv76Ja1pUJg+g2P6gaQhMJ5VMg-SGRE1kBEh4gocJ0bgVhUHqPFWgERJRhSYWlgSpRDjOK4xDhD4UXQMEhC6eEACCCYAbW9ZZA5K55uuHDAPkyDUCACDNq2LwxZEyDxehiVuThqCpWJhGZWpSrkbYVEFbRGWGCpTGsexnG4Nx856cahkOIUJmdSANiwi+OZ5lsPTNUw9XWm6AG3dG93IDmdlPjYPRvkZo3YXEAF8bAAlEEDIFoMJABeaXifoM3SdAClKataSMbGgrcNpIT0oQyFSFDGEQdhKVwNDU0rVlpGibldj5TR7ieCVz3lbpeMQ6TI2ucT7Nk9o03EQjUDzXl1GFcja3HBtm5bTthBAA) showing some code which has incorrect interface implementations which could error with more strict type checking.

Minimal example:

```typescript
interface IFoo {
	x: number | string;
}
class Foo implements IFoo {
	get x(): number { return 5;}
	// No setter!
}
const f: IFoo = new Foo();
// This also allows assigning strings to our read only number.
f.x = "um...";
```

I would like a way to opt into having Foo emit a compile error that it does not correctly implement IFoo due to incorrect setter type.

The same issue can be reproduced, even when using TS 5.1's variant setter types:

```typescript
// Even with unrelated getter and setter types, the setter type gets ignored when implementing the interface
interface IBaz {
	get x(): number;
	set x(value: string);
}

class Baz implements IBaz {
	get x(): number { return 5;}
	// No setter!
}

class IBaz2 implements IBaz {
	get x(): number{ return 5;}
	set x(value: object) {}
}
```
This case particularly seems like we should be able to give an error.
All these cases compile without errors on latest TypeScript (5.5.3), even with all strictness flags.


Note: I find it interesting that [splitting the interface into two interfaces then implementing both](https://www.typescriptlang.org/play/?noUncheckedIndexedAccess=true&target=9&useUnknownInCatchVariables=true&exactOptionalPropertyTypes=true&noImplicitOverride=true&noPropertyAccessFromIndexSignature=true&ts=5.5.3#code/JYOwLgpgTgZghgYwgAgJICE4C9kG8CwAUAJADmEYyAHgBQCUAXMiAK4C2ARtANxEC+RIqEixEKDNgDKFPEWIBnGbQBucADYsITeWCihSdXoQGEiAejPJJACwD2LNQBNk0KLajI4IZ49sR5LlBuUEQIanDyAZg4wGwADmoQbBDgARJYADRo0dKUBCTklLSMzOxcHrjIUBQsUCDIAKzcJibmljb2ToHBnt7Ivv7d7qHhkdnYAEzIsQlJKWBp0VnpubIFSvRMrJzQldVgtfVNJgobqhpayLYcAFYQCGB0eC2ChBZWdg7OILaUru5ZDgsSgDAL-EKEMIRRbYADM03iiWSqXGmVRq3yZA2JW25T2NTqjWackURRo5002l0+ieuBehCAA) of them produces very different results (more errors than expected instead of less). Maybe thats a bug? Seems related.

### 💻 Use Cases

1. What do you want to use this for?
Improving type safety for interface implanters.
The specific concreate case motivating this is that [Fluid Framewrok's tree](https://github.com/microsoft/FluidFramework/tree/main/packages/dds/tree) has a schema system that generates base classes in a strongly typed way for users to extend. We had a customer declare their schema based class implements an interface which included mutable fields. These fields had a type more general than the one our tree implements, which is fine for reading the data out of the tree, but allowed invalid data to be provided to the setters. Due to https://github.com/microsoft/TypeScript/issues/43826 we can't actually declare our desired setter type, but even the type we can declare is not getting type checking for its interface implementation. Also if someone added a custom setter override (to work around the linked issue), they wouldn't get any type checking that they got the input type for it correctly aligned with the interface.

3. What shortcomings exist with current approaches?
Compiler does not give errors for incorrect setters, which can result in errors going undetected and occurring at runtime. In some cases, this could result in data corruption.

5. What workarounds are you using in the meantime?
Manually check that setter types are correct when authoring them and in code review, and hope our users do the same.
Include additional runtime validation to help reduce risks of data corruption and instead produce informative runtime errors.