Skip to content

Some array methods should allow passing values of any typeΒ #42351

Closed

Description

Suggestion

πŸ” Search Terms

  • Array.prototype.includes should allow passing values of any type
  • Array.prototype.indexOf should allow passing values of any type

βœ… Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

Perhaps we could allow passing a generic type for array methods that search for the existence of something within an array as const so the developer can choose whether they want to allow this or not (and default to the current behavior).

The same would be useful for anything trying to find a value within an array (not just includes, but also indexOf)

πŸ“ƒ Motivating Example

Currently, I am experiencing this:

Playground link with relevant code

const a = ['one', 'two', 'three'] as const
const isFirstThreeNumbers = (type: string): type is typeof a[number] => a.includes(type)
//                                                                                 ^
// Argument of type 'string' is not assignable to parameter of type '"one" | "two" | "three"'.(2345)

isFirstThreeNumbers('four') // <-- false
isFirstThreeNumbers('three') // <-- true

We're getting a ts(2345) error because the array can only contain certain elements.

This limitation that I cannot check whether an arbitrary value exists within the array + have a type guard that stays in sync with the array.

My workaround:

const isFirstThreeNumbers = (type: string): type is typeof a[number] => a.includes(type as typeof a[number])

This is technically incorrect casting (because type could be anything), but I can't declare type as typeof a[number] because then consumers would only be allowed to pass values that are within the array values negating the usefulness of the function in the first place.

πŸ’» Use Cases

I think the use cases are very general. My specific use case is to determine whether a certain "exercise type" is within a given array:

const exerciseTypes = ['final', 'exercise', 'instruction'] as const
const isExerciseType = (type: string): type is typeof exerciseTypes[number] =>
  // .includes *should* allow you to pass any type, but it does not :-(
  exerciseTypes.includes(type as typeof exerciseTypes[number])

Activity

  1. RyanCavanaugh commented on Jan 14, 2021

    @RyanCavanaugh
    Member

    See #26255 / #14520

    Using type guard here is not sound due to its behavior in the false case; see #26255 (comment)

  2. kentcdodds commented on Jan 16, 2021

    @kentcdodds
    Author

    Gotcha! Thank you for the references Ryan Cavanaugh (@RyanCavanaugh)!

  3. locked as resolved and limited conversation to collaborators on Oct 21, 2025
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

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions