Union of array literal types has uncallable methods
Closed this issue ยท 3 comments
๐ Search Terms
"array literal method never"
๐ Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about distributive conditional types.
โฏ Playground Link
๐ป Code
declare let a: ["en"] | ["en-GB"] | ["en", "id-ID"] | ["zh-CN"];
a.includes("en");๐ Actual behavior
Error:
Argument of type '"en"' is not assignable to parameter of type 'never'.
๐ Expected behavior
No error
Additional information about the issue
The type of .includes is Array<T>.includes(searchElement: never, fromIndex?: number | undefined): boolean.
You'd think the type of searchElement would be "en" | "en-GB" | "id-ID" | "zh-CN", or at least string.
This is working as intended. When calling a method on a union, your parameters become an intersection. The first parameter of ["en"].includes() is typed "en", and of ["en-GB"].includes() it's typed "en-GB". The intersection "en" & "en-GB" results in never.
๐ค Thank you for your issue! I've done some analysis to help get you started. This response is automatically generated; feel free to ๐ or ๐ this comment according to its usefulness.
Similar Issues
Here are the most similar issues I found
- (74%) microsoft/typescript#52186: Union of 2 arrays leads to generic T=never inference
- (74%) microsoft/typescript_old#52186: Union of 2 arrays leads to generic T=never inference
- (73%) microsoft/typescript#44383: Array methods on union of tuples with empty tuple
- (73%) microsoft/typescript_old#44383: Array methods on union of tuples with empty tuple
- (71%) microsoft/typescript#48247: Too narrow typing for Array.prototype.includes
- (71%) microsoft/typescript_old#48247: Too narrow typing for Array.prototype.includes
- (71%) microsoft/typescript#48819: Array.prototype.includes type definition issue
- (71%) microsoft/typescript_old#48819: Array.prototype.includes type definition issue
- (71%) microsoft/typescript#53275: Array Includes incorrectly uses generic type
- (71%) microsoft/typescript_old#53275: Array Includes incorrectly uses generic type
- (71%) microsoft/typescript#36352: Array#includes argument type is too narrow
- (71%) microsoft/typescript_old#36352: Array#includes argument type is too narrow
- (70%) microsoft/typescript#61465: TS complains about assignability instead of narrowing
- (70%) microsoft/typescript_old#61465: TS complains about assignability instead of narrowing
- (70%) microsoft/typescript#46035: Readonly array `includes` should take valid primitive type
If your issue is a duplicate of one of these, feel free to close this issue. Otherwise, no action is needed.
This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.