The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
#extract the types of nodejs callback style function
9 messages · Page 1 of 1 (latest)
@stable gorge Here's a shortened URL of your playground link! You can remove the full link from your message.
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)
...```
You can choose specific lines to embed by selecting them before copying the link.
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
Create types which act like if statements in the type system.
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.
thanks!