-
-
Notifications
You must be signed in to change notification settings - Fork 216
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
intersect
of object
and record
does not work like Typescript
#491
Comments
Please try import * as v from 'valibot';
const Schema = v.object({ a: v.number() }, v.string()); |
While this improves validation the inferred output type is |
In my environment there is no problem with the current typing. |
Just to be clear I was trying out this import * as v from 'valibot';
const Schema = v.object({ a: v.number() }, v.string());
const obj: v.Output<typeof Schema> = { a: 1, b: "" } and then got
As for ideas my best guess is to replace the rest parameter with two new ones (maybe in an option object):
Then for example the schema v.object(
{ a: v.number() },
{
indexSignatures: [
[
v.special(
(input) => typeof input === "string" && input.startsWith("prefix")
) as v.SpecialSchema<unknown, `prefix${string}`>,
v.string(),
],
],
allowUnknownProperties: true,
}
) could parse But I have to admit that sounds like a lot of work and I get the feeling I have over done my types over here. |
I am currently rewriting the entire library. Most things will stay the same, but I expect the bundle size to be smaller, the performance to be better, and the types to be more accurate. I plan to rewrite this part of the library as well. My idea is to remove the |
I have investigated this issue further. There is a problem. Something like |
Furthermore, TypeScript does not allow us to define an object as |
I don't think it is feasible to check the key types for overlaps during the construction of the schema because the main purpose of this library is to check concrete values against schemas and not schemas against schemas (at that point you would basically implement TypeScript yourself). The user should be responsible for ensuring that the index signature do not overlap (or if they overlap the value type should match). As for the implicit conversion of numbers to strings the same logic as TypeScript's should be applied, so for example for the type |
I really don't know how we could implement that in a clean way. Feel free to figure it out after we are done with the rewrite in #502. |
I am not sure if there is still interest in this issue. I will close it for now. |
How about to using |
How do you think this solves the problem of this issue? |
I want to model a type that has both normal properties and an index signature, for example
{ a: number; [b: number]: string; }
. My best attempt so far is this one.Notice that Typescript does not show any errors but valibot gives this result which shows that the normal properties are checked against the index signature even though the the property (
"a"
) does not match the signature (number
).The text was updated successfully, but these errors were encountered: