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.

TypeScript: oneOf should accept readonly array

See original GitHub issue

Describe the bug

When handing in a readonly array to yup.mixed().oneOf, there is a TypeScript error:

The type ‘readonly ["pass", "fail", "any"]’ is ‘readonly’ and cannot be assigned to the mutable type any[]

To Reproduce

const validOptions = ["pass", "fail", "any"] as const

const schema = yup.mixed().oneOf(validOptions)

Expected behavior

The type should be adjusted to readonly any[] in order to indicate that the array doesn’t have to be mutable.

Platform (please complete the following information):

  • TypeScript 3.9.5
  • Yup 0.32.9

Additional context

I have not tested this with TypeScript 4.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:15
  • Comments:6

github_iconTop GitHub Comments

5reactions
jackokermancommented, Mar 25, 2021

You can also do:

const validOptions = ["pass", "fail", "any"] as const;

const schema = yup.mixed().oneOf([...validOptions]);

(so you don’t need to define the values twice)

1reaction
secure12commented, Dec 7, 2022

What is this closed? I’m on 0.32.11 and still facing this issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Enforce readonly array parameter for using the values of one ...
Specifically, I want to pass an array of objects to a function and its second parameter is the value of one of the...
Read more >
Read-Only Array and Tuple Types in TypeScript - Marius Schulz
The intersperse function accepts an array of elements of some type T and a separator value of the same type T . It...
Read more >
ReadonlyArray Lesson - Execute Program
Learn programming tools like JavaScript, TypeScript, SQL, and regular expressions fast. Interactive lessons with real code examples.
Read more >
Understanding and Embracing TypeScript's “readonly”
Understanding this lesson allows us to do things like efficiently assembling objects and arrays in mutable space inside a function–while ...
Read more >
Why you'll love Typescript's ReadonlyArray - Reddit
If a method accepts a read-only array, then it can't be sure that it won't be modified somewhere else. If it wants to...
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