使用嵌套类型参数而不在TS中声明两次

伊万·卡什塔诺夫(Ivan Kashtanov)

是否有可能做一些像这样的打字稿?

如果不是,是否有正式解释说明为何编译器无法推断嵌套类型参数?

这是我要实现的示例:

interface TestFilter {
    name?: string;
}

interface FilterComponent<F> {
    setFilter(filter: F)
}

class TestFilterComponent implements FilterComponent<TestFilter> {
    private filter: TestFilter;

    setFilter(filter: TestFilter) {
        this.filter = filter;
    }
}

// Can I use F without declaring it as another type parameter?
// Something like: class FilterWrapperComponent<FC extends FilterComponent<F>>
abstract class FilterWrapperComponent<F, FC extends FilterComponent<F>> {
    private sidebarFilter: FC;
    private modalFilter: FC;

    public passFilter(filter: F) {
        this.sidebarFilter.setFilter(filter);
        this.modalFilter.setFilter(filter);
    }
}

// I want to just write "extends FilterWrapperComponent<TestFilterComponent>"
// and get TestFilter as F automatically
class TestFilterWrapperComponent extends FilterWrapperComponent<TestFilter, TestFilterComponent> {

}

也可在操场上使用

那将是烧瓶

默认情况下,通用参数值为{}当省略参数时适用。可以使用通用参数defaults更改默认值

我只想编写“ extends FilterWrapperComponent”并自动将TestFilter获取为F

这是可能以相反的方式,让双方FilterComponent<TestFilter>TestFilter通过仅指定TestFilter

abstract class FilterWrapperComponent<F, FC = FilterComponent<F>> {}

class TestFilterWrapperComponent extends FilterWrapperComponent<TestFilter> {}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

有没有一种方法可以指定嵌套的泛型而不在Java 8+中两次指定类型

在IIFE中两次声明变量

两次使用休眠命名参数

如何在python sqlite3嵌套查询中两次传递相同的参数?

如何不在Linux中两次打开文件?

为什么在C的结构声明中两次使用typedef名称?

在javascript中检索深度嵌套的对象时不使用find两次

使用两次?在URL中

R 使用 purrr 对两次嵌套的列表求和

在一个ActionResult中两次运行.ExecuteStoreCommand时出错“参数已使用”

输入类型文件在Mozilla中触发两次(Windows)

使用不同的参数两次读取文件

构造函数仅使用参数运行两次

使用不同的参数调用函数两次

为什么不能将相同的函数与不同类型的参数一起使用两次?

两次模板<typename T> 声明

函数声明两次错误

如果C函数被两次调用,它将创建两次在函数中声明的变量吗?

为什么我可以声明一个变量的数据类型两次?

将函数作为参数类型脚本传递时,避免两次定义参数

PHP多维数组被嵌套两次

Writer Monad 嵌套两次

jQuery嵌套函数循环两次

在处理模板时,如何避免在函数头和主体中两次不同的时间声明同一类型?

两次定义字段时,猫鼬抛出“字段不在架构中”错误

如何使用Skycons在JavaScript中两次使用ID

php使用两次

两次使用(ngModelChange)

在python 3.6中两次使用train-test-split函数时应该作为输入参数传递什么