jeanpaul62
jeanpaul62

Reputation: 10581

TypeScript Generic type of a generic type

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> }
}

I also tried:

doSomething<Stream, T>(): Stream<T>

But doesn't work either.

Upvotes: 4

Views: 292

Answers (1)

jeanpaul62
jeanpaul62

Reputation: 10581

Answering myself: it is not possible yet (Jan 2019).

The issue is being discussed here: https://github.com/Microsoft/TypeScript/issues/1213

Upvotes: 3

Related Questions