[valibot] Types generic

Hi there ! I'm trying to create a small helper get (then post later). I think you can guess what is the endpoint.
const list = queryOptions({
queryKey: ["users"],
queryFn: () => get("/users", v.array(UserSchema)),
});

async function get<Schema extends v.AnySchema>(
path: string,
schema: Schema,
): Promise<v.InferOutput<Schema>> {
return fetch(`${API_URL}${path}`)
.then((res) => res.json())
.then((json) => v.parse(schema, json));
}
const list = queryOptions({
queryKey: ["users"],
queryFn: () => get("/users", v.array(UserSchema)),
});

async function get<Schema extends v.AnySchema>(
path: string,
schema: Schema,
): Promise<v.InferOutput<Schema>> {
return fetch(`${API_URL}${path}`)
.then((res) => res.json())
.then((json) => v.parse(schema, json));
}
I have the following type error:
frontend/src/core/users/users.api.ts|8 col 32-51 error| Argument of type 'ArraySchema<ObjectSchema<{ readonly id: StringSchema<undefined>; readonly email: SchemaWithPipe<[StringSchema<undefined>, EmailAction<string, undefined>]>; }, undefined>, undefined>' is not assignable to parameter of type 'AnySchema'. Types of property 'type' are incompatible. Type '"array"' is not assignable to type '"any"'.
frontend/src/core/users/users.api.ts|8 col 32-51 error| Argument of type 'ArraySchema<ObjectSchema<{ readonly id: StringSchema<undefined>; readonly email: SchemaWithPipe<[StringSchema<undefined>, EmailAction<string, undefined>]>; }, undefined>, undefined>' is not assignable to parameter of type 'AnySchema'. Types of property 'type' are incompatible. Type '"array"' is not assignable to type '"any"'.
I tried with BaseSchema and all kind of combinations, without success.. Any typescript master to help me? 🙏
12 Replies
Brendonovich
Brendonovich16h ago
I think AnySchema is a specific type of schema rather than the general type you're looking for Yeah try using extends v.BaseSchema
binajmen
binajmenOP16h ago
yeah it seems BaseSchema is more appropriate, but then I need to specify generic params and I get lost ;D
Brendonovich
Brendonovich16h ago
extends v.BaseSchema<any, any, any>
binajmen
binajmenOP16h ago
frontend/src/core/users/users.api.ts|11 col 35-47 error| Generic type 'BaseSchema<TInput, TOutput, TIssue>' requires 3 type argument(s). ok will try that
Brendonovich
Brendonovich16h ago
you're just looking for it to be a schema, doesn't matter what the generics are
binajmen
binajmenOP16h ago
Unexpected any. Specify a different type. mmmh
Brendonovich
Brendonovich16h ago
is that a ts error or a linter message?
binajmen
binajmenOP16h ago
linter
Brendonovich
Brendonovich16h ago
disable linter for that line lol any is what you want in this case
binajmen
binajmenOP16h ago
ok 😄
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
async function get<Schema extends v.BaseSchema<any, any, any>>(
path: string,
schema: Schema,
): Promise<v.InferOutput<Schema>> {
return fetch(`${API_URL}${path}`)
.then((res) => res.json())
.then((json) => v.parse(schema, json));
}
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
async function get<Schema extends v.BaseSchema<any, any, any>>(
path: string,
schema: Schema,
): Promise<v.InferOutput<Schema>> {
return fetch(`${API_URL}${path}`)
.then((res) => res.json())
.then((json) => v.parse(schema, json));
}
works fine ty !
Brendonovich
Brendonovich16h ago
noExplicitAny is an annoying rule anyway 😄
binajmen
binajmenOP16h ago
😂

Did you find this page helpful?