lonewarrior556
lonewarrior556

Reputation: 4479

Extract an interface/type from a typed function

If I have a typed function that I am importing called foo ie.

const foo(opts: {name:string, vers?:number, init:(...args)=>void }) => []

Is there a way to extract the type of opts from this function into it's own type/interface?

ie

interface IFooOpts = <TYPE OF foo(OPTS)>  

?

Upvotes: 2

Views: 593

Answers (1)

Titian Cernicova-Dragomir
Titian Cernicova-Dragomir

Reputation: 249636

You can use a conditional type and the inference behavior of conditional types to extract the type of the first argument in a type alias:

declare function foo(opts: {name:string, vers?:number, init:(...args)=>void }) : []

type IFooOpts = typeof foo extends (opts: infer U) => any ? U : never;

Upvotes: 4

Related Questions