Can use Generics defined in the Generic definition

I am trying to use a common set defined in a generic class definition:

type Foo<T : BoundType> = {
    bar : T
}

class Class<F : Foo<T : BoundType>> {
    method(arg : T) { ... }
}

Stream complains about syntax Class<F : Foo<T : BoundType>>

Is there a way to use the type T used in Foo in the class? The following works, but I'm trying to remove the need to repeat the type twice:

type Foo<T : BoundType> = {
    bar : T
}

class Class<T : BoundType, F : Foo<T>> {
    method(arg : T) { ... }
}

let x = new Class<ConcreteType, Foo<ConcreteType>>;
+4
source share
1 answer

You can use the Existential type (*) to tell Flow to specify a generic type parameter and $ ElementType in the "query" barproperty type:

type Foo<T : BoundType> = {
    bar : T
}

class TestClass<F : Foo<*>> {
    method(arg : $ElementType<F, 'bar'>) { }
}

declare var c: TestClass<Foo<ConcreteType>>;

Try here

** Perhaps there is a better way to get the type of a type parameter of a type, rather than querying a type of property bar.

*** Class TestClass, Class , .

0

Source: https://habr.com/ru/post/1696259/


All Articles