microsoft/TypeScript

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

https://www.typescriptlang.org/play/?#code/CYUwxgNghgTiAEEQBd5QFzwNoCIQDscBdeAH2z3wFoBxAIWLIoJwBp4cBLYKgSQBFG5XC3Y5kACyoAVABJDmhMQFsAzlQCyATQUilHAG6cqANQByunAC8pAYQslhlWgzEhVlgi7YdQVfgCintT0PjgAZjDB3mKcyHzSwWHuVAAsAIwAnElisDkcAA7xdABKxADcAFBQAHSc+JAArqCqABSUOACUlUA

๐Ÿ’ป 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.

This is another duplicate of #26255 / #14520.

๐Ÿค– 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

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.