Issue
Is there a way in Typescript to wrap all interface's values into some generic type to get another interface?
I have interface which represents an object with class constructors:
interface MyConstructors {
foo: typeof Foo;
bar: typeof Bar;
// ... etc (many lines here)
}
Then I create instances of these classes:
const instances = {
foo: new Foo(params),
bar: new Bar(params)
};
Interface of that object should look like
interface MyInstances {
foo: InstanceType<typeof Foo>;
bar: InstanceType<typeof Bar>;
// ... etc (many lines here)
}
As you can see it looks mostly the same as MyConstructors
. So I'm looking for some way to avoid that duplication.
Solution
You can use a mapped type:
type MyInstanceType = {
[key in keyof MyConstructors]: InstanceType<MyConstructors[key]>
}
// Same as:
// type MyInstanceType = {
// foo: Foo;
// bar: Bar;
//}
Answered By - Lesiak
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.