Skip to content
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

types: Lazy class is not exported #1954

Open
hotgazpacho opened this issue Mar 20, 2023 · 6 comments
Open

types: Lazy class is not exported #1954

hotgazpacho opened this issue Mar 20, 2023 · 6 comments

Comments

@hotgazpacho
Copy link

hotgazpacho commented Mar 20, 2023

Describe the bug
The Lazy class is not exported, while the factory function to create an instance of Lazy 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, and export it for use in another (e.g. a React component).

The following errors are produced:

Property 'builder' of exported class expression may not be private or protected.
Exported variable 'createSchema' has or is using name 'LazySpec' from external module "<redacted>/node_modules/yup/index" but cannot be named.
Property '_resolve' of exported class expression may not be private or protected.
Property 'optionality' of exported class expression may not be private or protected.
Exported variable 'createSchema' has or is using name 'CastOptions$1' from external module "<redacted>/node_modules/yup/index" but cannot be named.
Exported variable 'createSchema' has or is using name 'CastOptionalityOptions' from external module "<redacted>/node_modules/yup/index" but cannot be named.
Exported variable 'createSchema' has or is using name 'NestedTestConfig' from external module "<redacted>/node_modules/yup/index" but cannot be named.

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):

  • Browser [n/a]
  • Version [n/a]
  • TypeScript 4.9.5

Additional context
Yup 1.0.2

@pointnet
Copy link

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
  );
};

@ethanhoroschak
Copy link

ethanhoroschak commented Mar 29, 2023

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';

@oneyan1
Copy link

oneyan1 commented May 9, 2023

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;
};

@maapteh
Copy link

maapteh commented Jul 25, 2023

Is it possible to solve the issue, instead of forcing us to typecast?

@eugene-kim
Copy link

eugene-kim commented Jul 25, 2023

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

@jquense
Copy link
Owner

jquense commented Jul 25, 2023

PRs welcome if you want to move things along

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants