Open hotgazpacho opened 1 year ago
I have the exact same issue with the following code:
import { lazy, AnySchema } from 'yup';
export const LazyEmptySchema = (schema: AnySchema, emptySchema: AnySchema) => {
return lazy((value) =>
value === undefined || value === '' ? emptySchema : schema
);
};
Same issue here! Without Lazy export, it breaks all my types. The below code is what I had pre v1 but lib was removed and Lazy is no longer exported.
Cannot find module 'yup/lib/Lazy' or its corresponding type declarations.
import type Lazy from 'yup/lib/Lazy';
i have same issue here:
export const yupDynamicObject = (objectSchema: yup.AnyObjectSchema) => {
return yup.lazy((value) => {
if (!value || typeof value !== "object") {
return objectSchema;
}
return objectSchema.concat(
yup.object(Object.fromEntries(Object.keys(value).map((k) => [k, yup.mixed()])))
);
});
fixed from unknow type convert to AnyObjectSchema type, as:
export const yupDynamicObject = (objectSchema: yup.AnyObjectSchema) => {
const lazySchema = yup.lazy((value) => {
if (!value || typeof value !== "object") {
return objectSchema;
}
return objectSchema.concat(
yup.object(Object.fromEntries(Object.keys(value).map((k) => [k, yup.mixed()])))
);
}) as unknown;
return lazySchema as yup.AnyObjectSchema;
};
Is it possible to solve the issue, instead of forcing us to typecast?
Currently using patch-package
to deal with this until it's officially fixed. You just need to export Lazy
at the bottom export of node_modules/yup/index.d.ts
PRs welcome if you want to move things along
Describe the bug The
Lazy
class is not exported, while the factory function to create an instance ofLazy
is. All the other factory functions are paired paired with exports of the classes (NumberSchema
,StringSchema
,MixedSchema
, etc.) that they create.This is problematic when one uses
lazy()
to define a schema in one file, andexport
it for use in another (e.g. a React component).The following errors are produced:
To Reproduce
Define a schema using
lazy()
and export it.Expected behavior I am able to define a schema using
lazy()
and not receive any of the above TypeScript errors.Platform (please complete the following information):
Additional context Yup 1.0.2