Is it possible to wrap fields of a Typescript interface?

Viewed 46

I have 2 pretty similar interfaces:

interface Foo {
  value: boolean;
  time: string;
}

interface ReactiveFoo {
  value: BehaviorSubject<boolean>;
  time: BehaviorSubject<string>;
}

I would prefer to avoid the code duplication and make the ReactiveFoo interface to kind of extend Foo. I know that there are utility types such as Partial or Readonly

Maybe is it a way to do something like this (doesn't work):

type ReactiveFoo = Wrapped<Foo, BehaviourSubject>

P.S. Also BehaviourSubject is a generic type, that required an argument, not sure if it's possible to pass it without an argument

1 Answers

It is possible to hard code the BehaviorSubject type as such

type Wrap<Rec> = { [k in keyof Rec]: BehaviorSubject<Rec[k]> };

type Wrapped = Wrap<Foo>;

But I didn't find a way to actually pass a generic type (such as BehaviorSubject) as a parameter to another generic type (which would be Wrap in this particular example)

Related