Skip to content

Different return type inference when union with common members is reduced #1742

@dragomirtitian

Description

@dragomirtitian

Steps to reproduce

function validate() {
    if(Math.random() > 0.5) {
        return utilValidate();
    }
    return { valid: true };
};


declare function utilValidate(): {
    valid: boolean;
    msg?: undefined;
} | {
    valid: boolean;
    msg: string;
}

validate().msg; // Error in TSGO

Playground Link

Behavior with typescript@5.8

The return type of validate is { valid: boolean; msg?: undefined; } and validate().msg has no error

Behavior with tsgo

validate().msg raises the error: Property 'msg' does not exist on type '{ valid: boolean; }'. suggesting the return type of validate is { valid: boolean; }. If utilValidate is defined before validate there is no error

Maybe related to #1738

Metadata

Metadata

Assignees

Labels

Domain: Type CheckingRelated to type checking, grammar checkingType OrderingAn issue related to ordering of types

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions