Reputation: 4479
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
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