I have an interface with a bunch of fields, and I want to implement this interface in a class with all the same fields. The problem is that I have to redeclare each field of the interface in the class. Is there some shorthand way to achieve the same thing? It would achieve the same thing that the spread operator would in the object.
interface IUser {
id: number
name: string
age: number
// ... and more
}
// This is pretty redundant
class User implements IUser {
id: number
name: string
age: number
// ... and more
}
// Using some kind of spread mechanism
class User2 implements IUser {
...IUser
}
I am aware of the following post, which has a great solution for an interface as the implementation of another interface. But in my case, it’s a class implementing it, so I’m wondering if there’s a similar solution for classes:
Is there an equivalent of the Spread Operator in TypeScript for use with Interfaces?
I could not think of a way to do the same thing as in given the solution in my case.