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

Optional types in Tuple #311

Open
sagro111 opened this issue Sep 9, 2022 · 1 comment
Open

Optional types in Tuple #311

sagro111 opened this issue Sep 9, 2022 · 1 comment

Comments

@sagro111
Copy link

sagro111 commented Sep 9, 2022

Hi!

i try to type arrays items

I use tuple record
const tuple = t.Tuple(t.String, t.String.optional())

And when i try to check array like:
tuple.check(['Hello'])

I get error
Failed constraint check for [string, string | undefined]: Expected length 2, but was 1

I expect optional parameters to be ignored

@yuhr
Copy link
Collaborator

yuhr commented Sep 12, 2022

Optional runtype (which is generated by .optional() shorthand) doesn't work like that. As described in the error message, your tuple corresponds to [string, string | undefined] in TS's notation, that obviously doesn't accept ['Hello']. In such case you must use Union around two Tuples:

const tuple = t.Union(t.Tuple(t.String), t.Tuple(t.String, t.String))

We've noticed recently that the naming of Optional is somewhat misleading, so we're planning to have it reporting a static error to the user when misused.

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

2 participants