generics - TypeScript 中是否可以使用泛型类型别名?

标签 generics typescript alias

typescript 1.4 introduced type aliases .这些示例显示了如何使用像 type MyGreeter = Greeter<string> 这样的别名但是否可以使用通用别名?

以下示例不起作用:

type GenericAlias<T> = OriginalType<T>
type GenericAlias = OriginalType<T>

是否可以在不进行类型转换的情况下为泛型类型取别名?

最佳答案

从 TypeScript 1.6 开始,这是可能的。

// from #1616:
type Lazy<T> = T | (() => T);

var s: Lazy<string>;
s = "eager";
s = () => "lazy";

1.6 之前的答案

不,还没有。您可以在 issue #1616 中看到这方面的进展.

至于什么时候可以使用这个功能...

Lately we've been quite busy with ES6 alignment and the recently announced Angular 2.0 related features. We will get to (re)evaluating some of these type system specific issues but there's no concrete date for issues like this at the moment. - Source

关于generics - TypeScript 中是否可以使用泛型类型别名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29051899/

相关文章:

c# - 是否可以从成员中生成通用返回类型

java - 如何将实际方法作为参数传递给 Java 中的另一个方法?

angular - 如何连接 Output() 和 Input() 装饰器?

generics - TypeScript 中静态方法的抽象方法版本

mysql - 不是唯一的表/别名 :

c++ - std::thread 使用带有 ref arg 的 lambda 编译失败

java - 执行 map-reduce 操作的通用方法。 (Java-8)

typescript - TypeScript 中的接口(interface) + 新 vs 类 + 构造函数

bash - 在 bash 配置文件中定义函数时出现意外标记 `(' 附近的语法错误

mysql - Rails 中列名称的别名属性