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

Can't export MixedSchema when declaration=true set in tsconfig #1162

Closed
Gohsato opened this issue Dec 10, 2020 · 13 comments
Closed

Can't export MixedSchema when declaration=true set in tsconfig #1162

Gohsato opened this issue Dec 10, 2020 · 13 comments

Comments

@Gohsato
Copy link

Gohsato commented Dec 10, 2020

I'm trying to make a Typescript package with a yup schema.
My plan is to import this package in a couple projects and use the same validator.

I'm having some difficulty exporting the schema.
What I'm trying to do is export a mixed schema like so. But I'm getting a compiler error.

export enumSchema = yup.mixed<MyEnum>.().oneOf(Object.values(MyEnum))

The following example summarizes the problem I'm having.

// Set declaration:true in tsconfig.json
import { MixedSchema } from "yup";
export const a = new MixedSchema();

Which produces an error:

Exported variable 'a' has or is using name 'MixedSchema' from external module "/sandbox/node_modules/yup/lib/mixed"
but cannot be named.ts(4023)

I've also tried a few variations without success.

export const a = yup.mixed();
export const a: MixedSchema = yup.mixed();

How do I export 'a'?
Appreciate the help!

Here's the sandbox: https://codesandbox.io/s/pedantic-ishizaka-f7th5?file=/src/index.ts

@jquense
Copy link
Owner

jquense commented Dec 10, 2020

i'm not really clear on what this error means...usually it's because types aren't exported by the package, but they are so unsure why this is happening...

@Gohsato
Copy link
Author

Gohsato commented Dec 10, 2020

I just did some digging and these few lines may be the issue
https://github.com/jquense/yup/blob/master/src/mixed.ts#L38-L40

const Mixed: typeof MixedSchema = BaseSchema as any;
export default Mixed;

When Mixed is exported in this way only the class constructor is exported, not the class instance types. (This doc explains it nicely https://www.typescriptlang.org/docs/handbook/classes.html#constructor-functions).

One way to fix it would be to use namespace merging.

export class MixedSchema extends BaseSchema{}
export interface MixedSchema extends BaseSchema{
// migrate types from 'declare class MixedSchema'
}

I have created a sample implementation that shows how it will work in the sandbox.
https://codesandbox.io/s/falling-sun-c15fx

@jquense
Copy link
Owner

jquense commented Dec 10, 2020

hmm, the code is currently like this for a reason, Mixed, needs different types than Base, but i don't actually want them to be distinct classes. extending Mixed, should extend all other schema (it's weird)

@Gohsato
Copy link
Author

Gohsato commented Dec 10, 2020

So if I understand correctly:
Mixed should have the exact same implementation as Base. But Mixed has different type signatures for its functions.

@jquense
Copy link
Owner

jquense commented Dec 10, 2020

It's more strict Mixed has to be the same as Base. This is because mixed used to be the actual base class of all other schema, but the typescript typing isn't possible that way (the DT types also do this). So to work around this Mixed is typed as if it was distinct, but the Mixed.prototype.newMethod= newMethod would add newMethod to string, number, etc.

@cupcakearmy
Copy link

Same issue here :)

@jquense
Copy link
Owner

jquense commented Dec 14, 2020

PR's welcome if you know what might be wrong with how it's currently set up!

@Gohsato
Copy link
Author

Gohsato commented Dec 16, 2020

How about something like this?

export interface MixedSchema extends BaseSchema {...}
export const MixedSchema = BaseSchema

Here's a sandbox to demonstrate
https://codesandbox.io/s/async-violet-k70s9?file=/src/index.ts

I'll put up a PR if you think it might work

@jquense
Copy link
Owner

jquense commented Dec 16, 2020

If it works go for it, happy to take a PR

@sjohnsonaz
Copy link

I'm having this issue as well.

@svinci121
Copy link

I'm having this issue as well.

@zfael
Copy link

zfael commented Jun 9, 2021

Bumping to yup@0.32.9 fixed the issue! thanks, guys 🙌

@RuneSP
Copy link

RuneSP commented Jun 27, 2022

I'm getting the same error when using Ref, eg:

import { ref} from "yup";

export const fn = (cb: (ctx: SchemaContext) => unknown) => {
  return ref("$", { map: (ctx: any) => cb(ctx) });
};

TS4023: Exported variable 'fn' has or is using name 'Reference' from external module "node_modules/yup/index" but cannot be named.

@jquense jquense closed this as completed Aug 20, 2022
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