microsoft / microsoft/TypeScript

Ability to type check setters in interface implementations

未关闭
#59,331 2 条评论 5 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

Awaiting More Feedback Suggestion
主要语言
Go
星标
111k
派生
14.4k
平均合并
1 天 19 小时
30 天内合并 PR
117

描述

🔍 Search Terms

setter interface extends typecheck

✅ Viability Checklist
⭐ 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 showing some code which has incorrect interface implementations which could error with more strict type checking.

Minimal example:

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:

// 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 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 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.

  2. 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.

  3. 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.

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

先从 issue 中的最小 IFoo 和 IBaz 示例开始,然后查看链接的 issue #43826 以及 TypeScript 5.5.3 Playground 案例。当一个 opt-in 检查机制能够报告缺失或不兼容的 setter 实现,同时不改变生成的 JavaScript 或现有行为时,这项工作就完成了。

由索引模型根据 Issue 内容生成。

评估

技术栈
javascript, typescript
领域
compilers
Issue 类型
功能
难度
5/5
预计耗时
一周以上
活跃度
停滞
描述清晰度
基本清楚
新手友好度
25/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。