#Introduce types based on parameter from interface

30 messages · Page 1 of 1 (latest)

copper wyvernBOT
#

@vivid flame Here's a shortened URL of your playground link! You can remove the full link from your message.

Volnei#2684
vivid flame
copper wyvernBOT
balmy root
#

!ts

copper wyvernBOT
#
import { ZodError, ZodSchema, z } from 'zod'
//                                     ^^^^^
// Cannot find module 'zod' or its corresponding type declarations.

interface HandlerParams<Type> {
    parsed?: Type
}

interface InteceptInit<Schema extends ZodSchema> {
    handler: (params: HandlerParams<z.TypeOf<Schema>>) => Promise<any>
    schema?: Schema
    secure?: boolean
}


export function intercept<Schema extends ZodSchema>(init: InteceptInit<Schema>) {
    return async function (input: unknown): Promise<unknown> {
        try {
            let parsed
            if (init.schema) {
                parsed = await init.schema.parseAsync({
                    input
                })
            }
            return await init.handler({ parsed })
        } catch (error) {
            throw error
        }
    }
}
balmy root
#

this might work

#

idk whether it actually does tho

vivid flame
#

The code actually works but parsed still being a "any" type

balmy root
#

thonk

#

what type is schema in your code?

vivid flame
#

a ZodSchema

#

I trying to get parsed of type of z.infer<typeof mySchemaParameter>

#

I using to call intercept like this:

export const POST = intercept({
    handler: async ({  parsed }) => {
        console.log(parse.body.name) // but no completion for parse properties 
    },
    schema: z.object({
        body: z.object({
            name: z.string().min(2),
            code: z.string().max(6),
            email: z.string().email(),
            phone: z.string(),
            token: z.string(),
        }),
    }),
})
balmy root
#

hm

#

oh yknow what

#

oh

#

@vivid flame hover over intercept, what is Schema being inferred as?

vivid flame
#

@balmy root

balmy root
#

hm

#

weird cuz it should be inferring from the schema key

copper wyvernBOT
balmy root
#

!ts

copper wyvernBOT
#
import { ZodError, ZodSchema, z } from 'zod'
//                                     ^^^^^
// Cannot find module 'zod' or its corresponding type declarations.

type NoInfer<T> = [T][T extends T ? 0 : never]

interface HandlerParams<Type> {
    parsed?: Type
}

interface InteceptInit<Schema extends ZodSchema> {
    handler: (params: HandlerParams<z.TypeOf<NoInfer<Schema>>>) => Promise<any>
    schema?: Schema
    secure?: boolean
}


export function intercept<Schema extends ZodSchema>(init: InteceptInit<Schema>) {
    return async function (input: unknown): Promise<unknown> {
        try {
            let parsed
            if (init.schema) {
                parsed = await init.schema.parseAsync({
                    input
                })
            }
            return await init.handler({ parsed })
        } catch (error) {
            throw error
        }
    }
}
balmy root
#

edited version with NoInfer type to try and fix it

vivid flame
#

still not completing 😦

#

sorry my fault

#

works great!!! You are amazing!!!