Add option to specify array of exactly length n and add types for such
See original GitHub issueI 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:
- Created 4 years ago
- Reactions:3
- Comments:14 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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 😃
@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]”
@jquense any idea?