-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Calling indexOf and includes in a readonly array results in a "not assignable" error message #54422
Comments
To summarize past discussion around this: Ideally it should be an error to pass e.g. a string here, because that’s likely to be a mistake, so that’s why it doesn’t just take |
btw it’s not actually the |
Would it make sense to have overloads for the special case " |
TS doesn’t have anything resembling template specialization for generics so I don’t think that’s possible. It also doesn’t solve the equally common case where someone has e.g. |
interface Array<T> {
includes</* U super T */ U>(this: T extends U ? this : never, searchElement: U, fromIndex?: number | undefined): boolean;
}
interface ReadonlyArray<T> {
includes</* U super T */ U>(this: T extends U ? this : never, searchElement: U, fromIndex?: number | undefined): boolean;
} |
I don't think this is a real issue within a scope of readonly array with a set of compile-time fixed values. Consider the use case as you have mentioned: I have a string variable
So this type mismatch is not surely an issue that |
You’re right, and the “higher-level component” that’s taking notice of it is TypeScript—which is specifically designed to catch potential mistakes in your code at compile time based on the types used. Unfortunately that sometimes means this particular method is too narrow, due to inherent constraints in the type system. Anyway, I don’t make the rules, I’m only telling you how this discussion has gone in the past based on responses from team members. Don’t shoot the messenger. |
See for example #53904 (comment) |
Just wanted to address this - like I said above, TS doesn’t have template specialization, so this isn’t a distinction that can be made. The fixed readonly arrays necessarily have to share the same |
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Bug Report
I sometimes need to define a readonly array with a fixed set of values, and check if an unknown value matches one of those fixed values – for example, checking if an HTTP status code is one of the null body status codes (101, 103, 204, 205, 304) or redirect status codes (301, 302, 303, 307, 308).
But, after I put these status codes in a readonly array, and then call the
indexOf
method or theincludes
method on it, the compiler would generate an error message that the argument of theindexOf
method or theincludes
method "is not assignable to parameter of type …" followed by the constants in the array.🔎 Search Terms
🕗 Version & Regression Information
typescript
NPM package version: 5.0.4⏯ Playground Link
https://www.typescriptlang.org/play?#code/MYewdgzgLgBAFlKAHAylAhlArhAwiAEwFMIYBeGAbwCgYYwsAbRgIUIE8AuGAbRgEYADPwA0AwQGYxAJkEAWGYICsYifJgBdEbRgAnIgQCW+4FG581omGumrJdgOx2AHJu0BfGOlKhIUANzU1ABmWGCmhuAwhhAASgbGRKYAFNCYOPjE3AwAtgBGRLoAlFQ6+ti6YPCIqBjYeIQkAHT6RiZQTYbhjFjEEKl1GY1Fge5AA
💻 Code
🙁 Actual behavior
An error is marked under the
statusCode
argument in theincludes
call.The error message is
If I use
indexOf
instead, the error is the same.🙂 Expected behavior
The code should compile normally without error, because
includes
andindexOf
are both used to check if a user-supplied argument matches one of the values in the array. In pure JavaScript this operation is totally valid for variables of any types.The text was updated successfully, but these errors were encountered: