-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Union types not correctly handled in array.map #30271
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
Labels
Duplicate
An existing issue was already created
Comments
This is a workaround for the issue:
|
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Here's a workaround: /** Type workaround for https://github.com/Microsoft/TypeScript/issues/7294#issuecomment-465794460 */
type ArrayElem<A> = A extends Array<infer Elem> ? Elem : never
export function elemT<T>(array: T): Array<ArrayElem<T>> {
return array as any
} Example usage: type A = { x: number, y: string }
type B = { x: number, z: string }
type Subject = A[] | B[]
function foo (items: Subject) {
// items.map(item => item.x) // Type error
elemT(items).map(item => item.x) // Works!
} |
1 task
This is still an ongoing issue. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
TypeScript Version: playground / 3..3.333
Search Terms:
union type, array map
Code
Expected behavior:
here the
value
in the map expression should be of typestring[] | string[][]
Actual behavior:
Compilation error thrown:
Playground Link:
link
Related Issues:
The text was updated successfully, but these errors were encountered: