My attempt to utilize Parameters<> in order to mimic a function with a wrapper function seems to be facing some challenges. I had high hopes for the code below, but it's not working as expected. The "spawn" method has multiple overloads, and I assumed that Parameters<> would extract the parameters from the function implementation - one required parameter and two optional ones.
import { spawn } from "child_process"
function spawnWrapper(...args: Parameters<typeof spawn>) {
spawn(...args)
// ...
}
spawn("ls", ["-la"]) // => TypeScript compiler approves
spawnWrapper("ls", ["-la"]) // => Compiler throws an error saying Expected 3 arguments, but got 2.
UPDATE
Here's an alternative example without using an imported function:
function foo ( param: string ): void
function foo ( param: number, option: boolean ): void
function foo( param: string | number, option?: boolean ): void {}
function fooBar ( ...args: Parameters<typeof foo> ) {
foo( ...args )
}
foo( "baz" ) // Works fine
fooBar( "baz" ) // Compiler expects 2 arguments, but only receives 1.
UPDATE #2: Response from @ABOS
function foo ( param: string ): void
function foo ( param: number, option: boolean ): void
function foo( param: string | number, option?: boolean ): void
function foo( param: string | number, option?: boolean ): void {}
function fooBar ( ...args: Parameters<typeof foo> ) {
foo( ...args )
}
foo( "baz" ) // No issues here
fooBar( "baz" ) // All good now