Skip to content

Unification of functions on union types #34605

Closed
@pauldraper

Description

@pauldraper

TypeScript Version: 3.6.3

Search Terms:

Union type functions

Code

type A = number[] | string[];
const a = <A>[1];
a.forEach(element => console.log(element)); // error

Expected behavior:

No compile error. (element is inferred to be number | string)

Actual behavior:

Compile error. (Parameter 'element' implicity has 'any' type.)

Playground Link:

http://www.typescriptlang.org/play/?ssl=1&ssc=1&pln=4&pc=1#code/C4TwDgpgBAglC8UB2BXAtgIwgJwNoF0oAfKAZ2GwEskBzAgbgCgBjAeyXKgEMEoAeGAD5cARnxMuAOgBmrbAFEuzABYAKCABsIaCEmAJBUNh1ZbJG1jXVadegJR36UAPTOoObHMZA

Related Issues:


I'm unsure whether or not it is realistic to improve this. Recently I have been working with a number of A[] | B[] types and noticed that most array operations cannot be done on these.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions