question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add option to specify array of exactly length n and add types for such

See original GitHub issue

I am creating a factory which churns out schemas for objects, and one of the schema validations is an array of homogeneous types of exactly length n, if this wasn’t done at scale it’d be something like this:

type TExample = [string, string, string]

Yup has a nice-enough way of enforcing array length and type, let’s say for the first object object1 I need it to have an array of strings exactly 3 long:

const kvSettings = {
  object1: 3
}

// validation would then be
const shapeObject1 = yup.object().shape({
  args: yup.array().of(yup.string()).test('test arg length', '', (v: []) => v.length === kvSettings.object1),
})

That validation works but the inffered types incorrectly report a type of string[] for the array instead of [string, string, string]. This happens too with the less graceful double .min(3).max(3) syntax.

type Test = yup.InferType<typeof shapeObject1>

// type Test = {
//   args: string[];
// }

Yup should infer the types correctly as [string, string, string].

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:14 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
abdielalvarezcommented, May 6, 2020

Hello everyone, if what you wan’t is to define a maximum length do this const maxLength = 5 Yup.array().max(maxLength, 'too long')

if you want a minimum do this

const minLength = 1 Yup.array() .min(minLength, 'too short') .required('it is required')

at least you want to specify a type, this is my solution, i hope that helps someone 😃

2reactions
JimTevacommented, Dec 31, 2021

@tsujp not sure it is working properly with latest typescript. I have typescript 4.3.5 and this below code still gives type “boolean[]” instead of “[boolean, boolean, boolean, boolean, boolean]”

deliveryWeekDays: yup
    .array()
    .of(yup.boolean().required())
    .required()
    .length(5)

@jquense any idea?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to declare a Fixed length Array in TypeScript
The JavaScript array has a constructor that accepts the length of the array: let arr = new Array<number>(3); console.log(arr); // [undefined ...
Read more >
Add a generic way to specify length of a tuple type #26223
I'd like to see a way to specify the length of a tuple or array type when declaring it. For short tuples, it...
Read more >
Creating and filling Arrays of arbitrary lengths in JavaScript
The best way of creating an Array, is via a literal: const arr = [0,0,0]; Alas, that isn't always an option, e.g. when...
Read more >
Generate an array of size N according to the given rules
Given a number N, the task is to create an array arr[] of size N, where the value of the element at every...
Read more >
Documentation - Everyday Types - TypeScript
To specify the type of an array like [1, 2, 3] , you can use the syntax number[] ; this syntax works for...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found