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

TypeScript integration: how to use ArraySchema properly? #1839

Open
rockychordtea opened this issue Nov 22, 2022 · 2 comments
Open

TypeScript integration: how to use ArraySchema properly? #1839

rockychordtea opened this issue Nov 22, 2022 · 2 comments

Comments

@rockychordtea
Copy link

rockychordtea commented Nov 22, 2022

I didn't find any documentation on ArraySchema usage.
Specifically, it's not clear what its type parameters mean.

declare class ArraySchema<TIn extends any[] | null | undefined, TContext, TDefault = undefined, TFlags extends Flags = ''> ...

With just a single array-type parameter it doesn't work for obvious reason: the second parameter is required:

const foo: yup.ArraySchema<number[]> = yup.array().of(yup.number().defined()).defined()

triggering an error:

Generic type 'ArraySchema<TIn, TContext, TDefault, TFlags>' requires between 2 and 4 type arguments.

However, passing any as the second parameter doesn't make it work either:

const foo: yup.ArraySchema<number[], any> = yup.array().of(yup.number().defined()).defined()

and fails with this error:

Type 'ArraySchema<number[], AnyObject, "", "">' is not assignable to type 'ArraySchema<number[], any, undefined, "">'.
  Type '""' is not assignable to type 'undefined'.

Guessing further, after sett the third parameter to any removes error messages:

const foo: yup.ArraySchema<number[], any, any> = yup.array().of(yup.number().defined()).defined()

I wonder, what is the correct way to use this and what is the meaning of the parameters?

Version: 1.0.0-beta.8

@jquense
Copy link
Owner

jquense commented Nov 23, 2022

The last attempt with any works fine, ArraySchema<number[], any, undefined, ""> is probably the most correct. I need to update the generic defaults here tho so that you don't need to provide them like ObjectSchema

@karlhorky
Copy link
Contributor

In case you cannot use any in your codebase, unknown seems to also work:

const fileSchema: ArraySchema<File[], unknown, ''> =
  array()
    .of(
      object({
        ...
      }).required()
    ).required()

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

3 participants