You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Search Terms:
indexOf, too restrictive, searchElement
Expected behavior:
I expect Array.indexOf() to allow a searchElement with a type that includes and extends beyond the array. E.g. If the haystack is [2, 3], then I expect to the needle could be of type 1 | 2 | 3 and that a value of -1 would be returned if the needle is 1. But I do not expect a compiler error.
Actual behavior:
Compiler error.
Argument of type 'CoolNumbers' is not assignable to parameter of type '2 | 3'.
Type '1' is not assignable to type '2 | 3'. (2345)
Thanks for the root cause! I honestly don't understand all the terminology in that issue, but I feel like this is a fairly common use case for indexOf. Please let me know if I'm doing something wrong.
My current workaround is to typecast A as any to get past compilation errors.
TypeScript Version: 3.8.3
Search Terms:
indexOf, too restrictive, searchElement
Expected behavior:
I expect
Array.indexOf()
to allow asearchElement
with a type that includes and extends beyond the array. E.g. If the haystack is[2, 3]
, then I expect to the needle could be of type1 | 2 | 3
and that a value of-1
would be returned if the needle is1
. But I do not expect a compiler error.Actual behavior:
Compiler error.
Argument of type 'CoolNumbers' is not assignable to parameter of type '2 | 3'.
Type '1' is not assignable to type '2 | 3'. (2345)
Related Issues:
#29290
#35257
Code
Output
Compiler Options
Playground Link: Provided
The text was updated successfully, but these errors were encountered: