#Losing type inference somewhere
33 messages · Page 1 of 1 (latest)
your repro is not self-contained. could you provide the definitions for LoggerPayload, Result, and BaseError?
actually seems like the logger is irrelevant and BaseError could be anything, so Result is the important one. i'm guessing it's an Eitherish thing?
ya it is
i'm sorry you're right I didn't include those
but you can erase Result and BaseError and all that stuff and it should probably still do the same thing
oh Parsable too i guess
mind providing a more minimal repro that does this? i don't want to make any assumptions (also i'm lazy 😅)
Yeah I'll work on that shortly thanks
How's that?
You just need zod installed I believe
seems good, thanks
nah i only need the type definitions
which can be fetched automatically in the playground:
Preview:```ts
import {z} from "zod"
import {SafeParseReturnType} from "zod"
interface S2SafeParsable<Input, Output> {
safeParse: (
input: unknown
) => SafeParseReturnType<Input, Output>
}
type Handler<Input, Output> = (props: {
input: Input
}) => Promise<Output>
...```
at first glance i think the stuff like Parameters<H>[0]["input"] is probably not what you want to be doing, but i'll need to look closer to be specific
seems like you want def.resolve to itself be generic, right? the way you have it set up around line 26 it is not. it's just a concrete function type with H already applied via the original createDef call
should be like resolve: async <Something>(...) => ...
(i don't know what that Something should be yet)
Yeah that's the trick
and btw the implementation can just throw
the signature is the important part
right
i have to step away for a bit but i simplified the repro down to this, which might be easier to work through:
Preview:```ts
type Handler<Input, Output> = (props: {
input: Input
}) => Promise<Output>
type ReturnValueOfPromise<T> = T extends Promise<
infer U
? U
: never
declare const createDef: <
H extends Handler<any, any>
(_: {
handler: H
}) => {
resolve: (
_: Parameters<H>[0]["input"]
) => Promise<ReturnValueOfPromise<ReturnType<H>>>
...```
okay, back now (though i will have to leave again before too long)
how general-purpose do you need createDef to be? could it be set up such that resolve must always be a generic function with a single type parameter?
i ask because the most straightforward way to type it would be something like this:
Preview:ts ... declare const createDef: (_: { handler: Handler<any, any> }) => { resolve: <TE extends Event>( _: TE ) => Promise<Res<TE>> } ...
(tweaks may be needed when you go to implement it though)