Skip to content

Commit

Permalink
$mol_wire_async, $mol_wire_sync simplify types
Browse files Browse the repository at this point in the history
  • Loading branch information
zerkalica committed Oct 3, 2023
1 parent c495254 commit 0d8f20a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 25 deletions.
24 changes: 10 additions & 14 deletions wire/async/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,16 @@ namespace $ {
return fiber.async()
},

} ) as any as (
Host extends ( ... args: infer Args )=> infer Res
? Res extends Promise<any>
? Host
: ( ... args: Args )=> Promise< Res >
: {}
) & {
[ key in keyof Host ]: Host[ key ] extends ( ... args: infer Args )=> infer Res
? Res extends Promise<any>
? Host[ key ]
: ( ... args: Args )=> Promise< Res >
: Host[ key ]
}
} ) as any as MethodsResultPromisify<Host>

}


type PromiseWrap<P> = P extends PromiseLike<unknown> ? P : Promise<P>

type MethodsResultPromisify<Host extends Object> = {
[K in keyof Host]: Host[K] extends (...args: infer Args) => infer Res
? (...args: Args) => PromiseWrap<Res>
: Host[K]
}

}
18 changes: 7 additions & 11 deletions wire/sync/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,13 @@ namespace $ {
return fiber.sync()
},

} ) as any as (
Host extends ( ... args: infer Args )=> infer Res
? Res extends Promise< infer Res2 >
? ( ... args: Args )=> Res2
: Host
: {}
) & {
[ key in keyof Host ]: Host[ key ] extends ( ... args: infer Args )=> Promise< infer Res >
? ( ... args: Args )=> Res
: Host[ key ]
}
} ) as any as MethodsResultAwaited<Host>
}

type MethodsResultAwaited<Host extends Object> = {
[K in keyof Host]: Host[K] extends (...args: infer Args) => infer Res
? (...args: Args) => Awaited<Res>
: Host[K]
}

}

0 comments on commit 0d8f20a

Please sign in to comment.