A
arktype17mo ago
Dimava

Custom type-making functions

I want to make a chainable that can create types the same way type does How do I implement that?
thing // Thing<never, never>
.input('number%1>0') // Thing<number, never>
.output({ items: 'string[]' }) // Thing<number, {items: string[]}>
thing // Thing<never, never>
.input('number%1>0') // Thing<number, never>
.output({ items: 'string[]' }) // Thing<number, {items: string[]}>
1 Reply
Dimava
Dimava17mo ago
Seems like
// missing exports:
import type { PrecompiledDefaults as $, asIn, asOut, validateDefinition } from 'arktype'

class MyRpcQuery<In = undefined, Out = undefined> {
inType: Type<In> = type('undefined' as any)
outType: Type<Out> = type('undefined' as any)
callback!: (input: asOut<In>) => Promise<MyRpcResult<asIn<Out>>>

constructor() {}
in<T>(inType: validateDefinition<T, $>): MyRpcQuery<T, Out> {
this.inType = type(inType) as any
return this as any
}

out<T>(outType: validateDefinition<T, $>): MyRpcQuery<In, T> {
this.outType = type(outType) as any
return this as any
}

query(cb: (input: asOut<In>) => Awaitable<asIn<Out>>) {
this.callback = async function (inp) {
try {
const data = await cb(inp)
return { data, problems: undefined }
}
catch (error) {
return { data: undefined, problems: error }
}
}
return this
}
// missing exports:
import type { PrecompiledDefaults as $, asIn, asOut, validateDefinition } from 'arktype'

class MyRpcQuery<In = undefined, Out = undefined> {
inType: Type<In> = type('undefined' as any)
outType: Type<Out> = type('undefined' as any)
callback!: (input: asOut<In>) => Promise<MyRpcResult<asIn<Out>>>

constructor() {}
in<T>(inType: validateDefinition<T, $>): MyRpcQuery<T, Out> {
this.inType = type(inType) as any
return this as any
}

out<T>(outType: validateDefinition<T, $>): MyRpcQuery<In, T> {
this.outType = type(outType) as any
return this as any
}

query(cb: (input: asOut<In>) => Awaitable<asIn<Out>>) {
this.callback = async function (inp) {
try {
const data = await cb(inp)
return { data, problems: undefined }
}
catch (error) {
return { data: undefined, problems: error }
}
}
return this
}
Want results from more Discord servers?
Add your server