Here is the code snippet I am currently working with:
class Route {
constructor(
public method: 'get' | 'post' | 'update' | 'delete',
public path: string,
public handler: () => string,
) {}
}
class Router {
constructor(private routes: (Route | Parameters<typeof Route.constructor>)[] = []) {}
}
My goal is to make the Router
class capable of accepting an array that includes either a Route
object or just an array of arguments for constructing new Route
instances. Here's how it should work:
const router = new Router([
new Route('get', '/', () => 'Hello, world!'),
// or
['get', '/', () => 'Hello, world!'],
]);
I have tried using the Parameters
feature to retrieve function parameters as tuples. However, when attempting to apply this to constructors, I encounter the following error message:
Type 'Function' does not satisfy the constraint '(...args: any) => any'.
Despite searching online extensively, I have not found a solution that addresses my specific scenario.
Therefore, I am seeking advice on whether there is a way to accomplish this task successfully.