microsoft / microsoft/TypeScript

Allow inference of class generics when declaring an interface

未关闭
#32,044 1 条评论 6 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

Awaiting More Feedback Suggestion
主要语言
Go
星标
111k
派生
14.3k
平均合并
2 天 4 小时
30 天内合并 PR
132

描述

Search Terms

interface, inference, extending class

Suggestion

I'd like to be able to strongly type an interface such that when it is used on a class that has a generic I can infer the type of that generic to then use in the declaration of the interface.

Use Cases

I want to use this within a library that exposes both an abstract class and an optional interface that developers can opt-in to use. The abstract class has one generic parameter, and in order to correctly type the interface without duplicate generic declaration I want to be able to infer the abstract class' generic type.

Currently the inheriting class declarations must duplicate their generic type declaration.

This issue is similar to https://github.com/microsoft/TypeScript/issues/26242 I think, but I don't see how the resolution of that issue will fix this one.

Examples

abstract class Base<T> {
    // implementation omitted for brevity
}

// ⬇️this is the proposed new syntax ⬇️
interface Remap<T> extends Base<infer U> {
    remap(input: U): T;
}

class Foo extends Base<string> implements Remap<number> {

    public remap(input: string): number {
        return input;
    }

}

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, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

贡献指南

打开贡献指南

从这里开始

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

调研方向

Issue 中没有标识出源文件、测试或编译器入口点。首先阅读提议的 TypeScript 语法和相关 issue #26242,然后跟踪当前如何检查泛型继承和接口声明。团队定义并实现预期的推断行为,并为所示示例及其边界情况提供覆盖后,即可视为完成。

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

评估

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

把新 issue 发到你的邮箱

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