microsoft / microsoft/TypeScript
Enables `implements` on abstract classes with protected members (usefull for generic mixins)
还没有人认领这个 Issue。
- 主要语言
- Go
- 星标
- 111k
- 派生
- 14.3k
- 平均合并
- 2 天 4 小时
- 30 天内合并 PR
- 132
描述
### 🔍 Search Terms
implement abstract class with protected
### ✅ 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
Enable doing the following (currently this raises an error):
```ts
abstract class Base {
protected abstract foo(): void
}
class B implements Base {
protected foo() {}
}
```
### 📃 Motivating Example
This would be useful to declare "protected" interfaces, when we can't use `extends` as the child class is already extending another.
```ts
// interface
abstract class Base {
protected abstract foo(): void
}
class C {}
class B extends C implements Base {
protected foo() {}
}
```
Such need is frequent when using generic mixins: indeed, type manipulations inside generic functions (and mixins) are often quite troublesome, with unwanted type expansions/generalization (?). Then this is often better explicitly describe the returning type.
To do so, `abstract class` is the only way to properly describe the properties added by the mixin. However, if such class has protected properties, this raises TS error as classes with protected properties are no longer duck typing but "nominal" (?).
```ts
type Cstr = new(...args:any[]) => T;
type Mergeany,
B extends new(...args:any[])=>any
> = Omit & (new() => (InstanceType & InstanceType))
function foo>(klass: T): Merge {
return class B extends klass implements Base {
protected foo() {}
}
}
class X extends foo(C) {}
```
### 💻 Use Cases
1. What do you want to use this for?
Easily describing generic mixins return type.
2. What shortcomings exist with current approaches?
Can't describe protected properties.
3. What workarounds are you using in the meantime?
Dirty casting, removing some type checks inside the generic mixin.
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
该 issue 未提供文件或测试路径。首先定位 class `implements` 与受保护成员兼容性的类型检查逻辑和测试,然后比较抽象类与泛型 mixin 示例。完成的标准是:示例无需更改生成的 JavaScript 即可通过类型检查,并且包含回归覆盖。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- typescript
- 领域
- compilers
- Issue 类型
- 功能
- 难度
- 5/5
- 预计耗时
- 一周以上
- 活跃度
- 停滞
- 描述清晰度
- 描述清楚
- 新手友好度
- 35/100