In my current project, I have a Class that contains a method which I would like to utilize in a de-structured manner, primarily due to the presence of multiple optional arguments. To maintain strict typing, I have created an Interface to define these arguments.
Although the current setup is functional, it can be a bit cumbersome, especially when most functions require de-structured arguments.
My query is: Can we define an interface within a class?
The motivation behind this is simply to enhance code readability and maintenance. In larger classes, I find myself having to scroll outside the class to modify a "function" interface, whereas having the interface alongside the function would be much more organized.
Here's an example of the current setup that works:
interface LoadUsersInterface {
status: 'active' | 'inactive',
options?: QueryOptions
}
class UserService {
loadUsers({ status, options }: LoadUsersInterface) {
...
}
}
And here's what I envision and would like to achieve:
class UserService {
interface LoadUsersInterface {
status: 'active' | 'inactive',
options?: QueryOptions
}
loadUsers({ status, options }: LoadUsersInterface) {
...
}
}
Note: This scenario is happening within an Angular 11 application. While there may be specific features in Angular that I'm unaware of, I believe this is more of a JavaScript / TypeScript related question.