-
-
Notifications
You must be signed in to change notification settings - Fork 212
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Best practice for valibot to work with typescript's isolated declarations feature #968
Comments
Valibot offers |
|
I have conducted a thorough study and found that |
This causes an type error because const argsFnSchema: v.GenericSchema<(args: number[]) => boolean> = v.pipe(
v.function(),
v.args(v.tuple([v.array(v.number())])),
v.returns(v.boolean()),
) |
Yes,
import * as v from 'valibot';
const argsFnSchema: v.GenericSchema<
(...args: unknown[]) => unknown,
(yourArg: number[]) => boolean
> = v.pipe(
v.function(),
v.args(v.tuple([v.array(v.number())])),
v.returns(v.boolean())
); |
Thanks for your answer, but when the schema is complex, it will become more complicated: type ComplexSchemaType = {
fn: (arg: string) => boolean
argsWithFn: (arg: (arg: boolean) => string) => void
}
const ComplexSchema: v.GenericSchema<?, ComplexSchemaType> = v.strictObject({
fn: v.pipe(
v.function(),
v.args(v.tuple([v.string()])),
v.returns(v.boolean()),
),
argsWithFn: v.pipe(
v.function(),
v.args(
v.tuple([
v.pipe(
v.function(),
v.args(v.tuple([v.boolean()])),
v.returns(v.string()),
),
]),
),
),
}) |
You can always infer the correct input and output type by using import * as v from 'valibot';
const ComplexSchema = v.strictObject({
fn: v.pipe(
v.function(),
v.args(v.tuple([v.string()])),
v.returns(v.boolean())
),
argsWithFn: v.pipe(
v.function(),
v.args(
v.tuple([
v.pipe(
v.function(),
v.args(v.tuple([v.boolean()])),
v.returns(v.string())
),
])
)
),
});
type Input = v.InferInput<typeof ComplexSchema>;
type Output = v.InferOutput<typeof ComplexSchema>; |
Yeah. But, this doesn't seem to help with explicitly declaring the type, because ts error |
The idea is to hover over |
I still think it would be best to disable the isolated declaration rules for such cases, but not sure if this is possible. |
Actually, I found that type Output = {
fn: (args_0: string) => boolean;
argsWithFn: (args_0: (...args: unknown[]) => unknown) => unknown;
} Is this the expected behavior? Is there a problem with my writing for the |
Thank you very much. |
With enabling https://www.typescriptlang.org/tsconfig/#isolatedDeclarations,
tsc
require to write code with explicit type annotations:For exmaple:
The case in README.md
For small schemas, I could just type them there. But I have very big schemas which isn't proper to manually write them.
zod
has a helper typeZodType
to infer schemas from types. So for zod, it could be written asThe text was updated successfully, but these errors were encountered: