TypeScript泛型类型的泛型类型 [英] TypeScript Generic type of a generic type
本文介绍了TypeScript泛型类型的泛型类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以传入需要另一种通用类型的通用类型?这是我想做的:
Is it possible to pass in a generic type that requires another generic type? Here's what I want to do:
abstract class MyClass<Stream> {
abstract doSomething1(): Stream<number> // TS error here: Stream does not take generic parameter
abstract doSomething2(): Stream<string> // TS error here: Stream does not take generic parameter
}
// Implementations of the abstract class:
class MyObservableClass extends MyClass<Observable> {
doSomething1() { // returns Observable<number> }
doSomething2() { // returns Observable<string> }
}
class MyPromiseClass extends MyClass<Promise> {
doSomething1() { // returns Promise<number> }
doSomething2() { // returns Promise<string> }
}
我也尝试过:
doSomething<Stream, T>(): Stream<T>
但也不起作用.
推荐答案
回答自己:目前还不可能(2019年1月).
Answering myself: it is not possible yet (Jan 2019).
此处正在讨论该问题: https://github.com/Microsoft/TypeScript/第1213个问题
The issue is being discussed here: https://github.com/Microsoft/TypeScript/issues/1213
这篇关于TypeScript泛型类型的泛型类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文