Skip to content

Allow passing ReadonlyArray<T> and ReadonlySet<T> to expect.toBeOneOf #10264

Description

@Bertie690

Clear and concise description of the problem

Currently, the expect.toBeOneOf matcher does not allow passing in ReadonlyArrays or ReadonlySets in TypeScript without giving type errors.

The actual implementation does not modify either argument type in any way, shape or form - this is purely a typing issue.

Moreover, this would also prevent the asymmetric matcher from accidentally mutating its passed-in arguments (which could easily corrupt shared global state inside noIsolate runs).

Suggested solution

Change the typing to use ReadonlyArray<T> and ReadonlySet<T> instead of Array<T> and Set<T>.

Alternative

Don't pass in readonly arrays or sets (though this isn't exactly desireable)

Additional context

TS Playground link

Validations

Remarks

I am willing to work on this as a feature if need be - the fix should be very easy.

Activity

  1. added theissue type on May 4, 2026
  2. changed the title [-]Allow passing `ReadonlyArray<T>`s to `expect.toBeOneOf`[/-] [+]Allow passing `ReadonlyArray<T>` and `ReadonlySet<T>` to `expect.toBeOneOf`[/+] on May 4, 2026
  3. hi-ogawa commented on May 5, 2026

    @hi-ogawa
    Collaborator

    I think it's fine to change, but there might be other places you want the same like:

    import { it, expect } from 'vitest';
    
    // imagine this is an external constant used elsewhere
    const ALLOWED = ['A', 'B'] as const;
    
    it('should be A or B', () => {
      expect('A').toBe(expect.toBeOneOf(ALLOWED));
      expect(['A']).toEqual(expect.arrayContaining(ALLOWED))
    });

    https://www.typescriptlang.org/play/?ssl=10&ssc=1&pln=1&pc=1#code/JYWwDg9gTgLgBAbzsGAaOBTAHmDBjeAXzgDMoIQ4ByANxQwGcYqBuAWAChOB6b5EAIYBzYADsMcGAAtgDZHIGjMWGBiiiBAGzh4IopovgBXBhgAmmTaYDuUtRk6798AIIAZNwHkA6gFEAInAAvHAA2lQuVOhUAEJUALpwAnJOTOxcHCgAFFQMUhBGmhYARhIucNBwcehZAJTBAHyInHDKuAQ5kbUAdDAQMRhZ2O0wvf0YnuKeJFnuXn7+tbXprcP4MFnhkfE9fb4AjkZaQzjr3QJQUAIAngDCejACYmJCsx4+AUuchMucQA

  4. Bertie690 commented on May 6, 2026

    @Bertie690
    ContributorAuthor

    Good catch.

    this might warrant a sweep over all the asymmetric matchers to see which ones deserve this treatment.

  5. moved this to P2 - 2 in Team Boardon May 6, 2026
  6. added
    p2-to-be-discussed
    on May 6, 2026
  7. hi-ogawa commented on May 7, 2026

    @hi-ogawa
    Collaborator

    @Bertie690 Feel free to send a PR to test out the change.

  8. added a commit that references this issue on May 18, 2026
    fed1125
  9. locked and limited conversation to collaborators on Jul 30, 2026
  10. added a commit that references this issue on Sep 9, 2026
    aa7eaec
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement: pending triagep2-to-be-discussed

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions