Validation transformer throwing TS error on nested data access #1020
-
Hi there! I'd like to create a following schema util: const unwrapResponse = <T extends v.GenericSchema>(schema: T) =>
v.pipe(
v.object({
body: v.object({
data: schema,
}),
}),
v.transform((input) => {
return input.body.data;
}),
); This helper is meant to:
The runtime validation works perfectly, but TypeScript is throwing an error in the transform function: Here is the playground example Any suggestions on how to fix this? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Without further investigation, it appears that TS has problems resolving a generic schema in this case. In the long run, we should investigate whether this is due to our implementation or a limitation of TS. A simple workaround is to keep only the input (and optionally the output) generic, but not the pass schema itself: const unwrapResponse = <TInput, TOutput>(
schema: v.GenericSchema<TInput, TOutput>
) =>
v.pipe(
v.object({
body: v.object({
data: schema,
}),
}),
v.transform((input) => {
return input.body.data;
})
); |
Beta Was this translation helpful? Give feedback.
-
Good news! I fixed some types in |
Beta Was this translation helpful? Give feedback.
Without further investigation, it appears that TS has problems resolving a generic schema in this case. In the long run, we should investigate whether this is due to our implementation or a limitation of TS.
A simple workaround is to keep only the input (and optionally the output) generic, but not the pass schema itself: