eclipse-langium / langium

Next-gen language engineering / DSL framework
https://langium.org/
MIT License
663 stars 61 forks source link

Incorrect array type inference #1505

Closed msujew closed 4 weeks ago

msujew commented 1 month ago

Langium version: 3.0 Package name: langium

Steps To Reproduce

Create a parser rule like:

A: items+=ID items+=NUMBER;
terminal ID returns string: ...;
terminal NUMBER returns number: ...;

This generates an unexpected type, see below.

The type below is both unexpected as well as difficult to work with. A unioned array element type would be preferable here.

The current behavior

The parser rule above will be generated as:

interface A extends AstNode {
  $type: 'A';
  items: Array<number> | Array<string>
}

The expected behavior

The expected type would be

interface A extends AstNode {
  $type: 'A';
-  items: Array<number> | Array<string>
+  items: Array<number | string>
}

Note that the type union has been pulled into the array.