Skip to content

Incorrect types inferrence for parents with own properties #756

@pluralia

Description

@pluralia

When a parent has no own properties, we generate a type Parent :

Parent:
    ChildA | ChildB;

ChildA:
    'a' a=NUMBER commonProp=ID;

ChildB:
    'b' b=NUMBER commonProp=ID;

generates

export type Parent = ChildA | ChildB;

export interface ChildA extends AstNode {
    a: number
    commonProp: string
}

export interface ChildB extends AstNode {
    b: number
    commonProp: string
}

Besides, common children properties like commonProp will be lifted automatically. However, as only a parent has own properties, we generate an interface Parent:

Parent:
    parentProp=ID (ChildA | ChildB);

// ChildA and ChildB are without changes

generates

export interface Parent extends AstNode {
    parentProp: string
}

export interface ChildA extends AstNode {
    a: number
    commonProp: string
}

export interface ChildB extends AstNode {
    b: number
    commonProp: string
}

You can notice, that ChildA and ChildB don't extend Parent and their commonProp is not lifted in Parent.

Langium version: 0.5.0
Package name: langium

The current behavior

Inferred Parent -- ChildA -- ChildB hierarchy is wrong when Parent has own properties (look above).

The expected behavior

export interface Parent extends AstNode {
    parentProp: string
    commonProp: string
}

export interface ChildA extends Parent {
    a: number
}

export interface ChildB extends Parent {
    b: number
}

The solution to the problem requires restoring properties lifting algorithm.

Metadata

Metadata

Assignees

No one assigned

    Labels

    astAST structure related issuetypesTypes related issue

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions