#extract the types of nodejs callback style function

9 messages · Page 1 of 1 (latest)

tacit lanternBOT
#

@stable gorge Here's a shortened URL of your playground link! You can remove the full link from your message.

ricmed#0

Preview:```ts
import {readFile} from "node:fs"

type Arg1<Fn> =
Fn extends (
input: infer X, Opts: any, CB: any
) => void ?
X :
never

type Opts<Fn> =
Fn extends (
input: any, Opts: infer X, CB: any
) => void ?
X :
never

let file: Arg1<typeof readFile>

// I'm getting the callback type (3rd parameter), not the options type (2nd parameter)
...```

glass thunder
#

it's not possible to extract the signature of a function that uses overloads

#

readfile has 4 possible overloads

#

while the 1st parameter is always the same (path: PathOrFileDescriptor) there are no problems

#

but as soon as the overloads have a different type for the other parameters, you'll always get the type of the last overload

#

When inferring from a type with multiple call signatures (such as the type of an overloaded function), inferences are made from the last signature (which, presumably, is the most permissive catch-all case). It is not possible to perform overload resolution based on a list of argument types.

stable gorge
#

thanks!