Imported package has incorrect inferred typings for optional and nullable properties #908
-
Hi, wasn't sure if this was worthy of being submitted as an issue or if it is some problem on my side. Looking for some advice. I have an npm package that I made that uses Valibot for type schemas. It previously used Zod, but I wanted to reduce the overall bundle size of my React app, so wanted to make the switch to Valibot. Recently I reinstalled all dependencies on my React app and suddenly the inferred types of my optional and nullable properties were no longer optional and did not accept null as a value either. This issue is for hundreds of types, so it is not an isolated issue. This worked up until a few days ago, so I thought maybe it was due to the recent 1.0.0-beta.3 release. I spent a bunch of time trying to resolve this but have not been able to. Doesn't seem to be an issue with my tsconfig or TypeScript version(the package was built using 5.6.2 but my React app uses 5.6.3). Here is a very simplified version of a schema definition in my package: export const GetNetworksFilterSchema = v.object({
name: Common.OptionalNullableStringArraySchema,
});
export type GetNetworksFilter = v.InferOutput<typeof GetNetworksFilterSchema>; Here export const StringSchema = v.pipe(v.unknown(), v.transform((value) => String(value)));
export const StringArraySchema = v.array(StringSchema);
export const NullableStringArraySchema = v.nullable(StringArraySchema);
export const OptionalNullableStringArraySchema = v.optional(NullableStringArraySchema); As you can see I am clearly defining that property When I build the package, the resulting index.d.ts file also shows the correct type being inferred for GetNetworksFilter. However, when I import this package into my React app, suddenly the field is no longer optional and no longer accepts null or undefined. I have verified that the index.d.ts file in my React app exactly matches the one in my built package, so it merely comes down to how TypeScript is inferring those types in my React app. Steps I've already taken to try and resolve this issue:
I am using Valibot v1.0.0-beta1 for my package and TypeScript 5.6.3 in my React app. Any assistance would be greatly appreciated! I've reverted back to using my Zod package for the time being as I am blocked here. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Thank you for reaching out and sorry for this problem! Can you provide me with a minimal reproduction? For example, a GitHub repository or code snippets with step-by-step instructions on how to reproduce the problem. This would help me to investigate what is happening here. |
Beta Was this translation helpful? Give feedback.
Not entirely sure where the issue was coming from, but it has been resolved by simply rebuilding and publishing the package. Maybe it was caused by the version mismatch between TypeScript or Valibot, but I don't know for certain. Thought that perhaps it was due to the package having the "types" compiler option set to just the types from the
jest-dom
, but it seems that that isn't the case. I'll investigate further and open an issue if necessary, but it seems like at the very least it's working fine now. I was not able to replicate the same issue with a minimal reproduction, so perhaps it was just related to the versions being mismatched or some other dependency.