Skip to content

[BUG] Issue with union and literal types #18365

Description

There seems to be a problem with unions and literal types when both types of the union contain a field with the same name.

TypeScript Version: 2.5.2

Code
The small example

class A {
    field: 42;
}

class B {
    field: number;
    otherfield: number;
}

// Gives an error
let x: A | B = {
    field: 42
};
// Type '{ field: number; }' is not assignable to type 'A | B'.
//  Type '{ field: number; }' is not assignable to type 'B'.
//    Property 'otherfield' is missing in type '{ field: number; }'.

The simple example of a more realistic situation (typeorm-ish code)

type OrderBy = 'ASC' | 'DESC';

type SimpleOrderByCondition = {
    name?: OrderBy;
    randomField?: OrderBy;
};

type OrderByCondition = SimpleOrderByCondition | (() => SimpleOrderByCondition);

// Works OK as expected
let orderBy1: OrderByCondition = {
    randomField: 'ASC'
};

// gives an error. But, it should work.
let orderBy2: OrderByCondition = {
    name: 'ASC'
};

Expected behavior:
No error

Actual behavior:

[ts]
Type '{ name: string; }' is not assignable to type 'OrderByCondition'.
Type '{ name: string; }' is not assignable to type '() => SimpleOrderByCondition'.
Type '{ name: string; }' provides no match for the signature '(): SimpleOrderByCondition'.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugA bug in TypeScriptDuplicateAn existing issue was already created

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions