Sealleci
Sealleci
Aarktype
Created by Sealleci on 2/2/2025 in #questions
How to properly infer the return type of a generic function returning ArkType schemas?
4 replies
Aarktype
Created by Sealleci on 2/2/2025 in #questions
How to properly infer the return type of a generic function returning ArkType schemas?
Yes, when I saw the changelog for version 5.8 today, I was extremely excited, as if this update was tailor-made for me (of course, that's just a joke 😉 And that's my workaround now:
interface ResponseMap {
'A': AResponse | null
'B': BResponse | null
'C': CResponse | null
}
async function myFetch<T extends QueryType>(queryType: T): Promise<ResponseMap[T]> {
...
switch(queryType){
case 'A': {
const parsedJson = arkType('string.json.parse').to(
queryModule.aResponse
)(rawJson)

if (!(parsedJson instanceof arkType.errors)) {
return parsedJson
}

break
}
case 'B': {...}
case 'C': {...}
default:
break
}
return null
}
interface ResponseMap {
'A': AResponse | null
'B': BResponse | null
'C': CResponse | null
}
async function myFetch<T extends QueryType>(queryType: T): Promise<ResponseMap[T]> {
...
switch(queryType){
case 'A': {
const parsedJson = arkType('string.json.parse').to(
queryModule.aResponse
)(rawJson)

if (!(parsedJson instanceof arkType.errors)) {
return parsedJson
}

break
}
case 'B': {...}
case 'C': {...}
default:
break
}
return null
}
4 replies