Skip to content

Different error message when object is assigned to a union that contains undefined #1790

@dragomirtitian

Description

@dragomirtitian

Steps to reproduce

export type Data = {
    rows: Array<{ foo: string }>
} | {
    columns: number[];
}

declare function callback(response: Data | undefined) : void
callback({
    rows: [
        {}
    ]
});

Playground Link

Behavior with typescript@5.8

We get an error on {} telling us the property foo is missing:

generic/some-code.ts:10:9 - error TS2741: Property 'foo' is missing in type '{}' but required in type '{ foo: string; }'.

10         {}
           ~~
....

Behavior with tsgo

We get an error on the whole object literal making it more difficult to find the cause:

generic/some-code.ts:8:10 - error TS2345: Argument of type '{ rows: {}[]; }' is not assignable to parameter of type 'Data | undefined'.
  Types of property 'rows' are incompatible.
    Type '{}[]' is not assignable to type '{ foo: string; }[]'.
      Property 'foo' is missing in type '{}' but required in type '{ foo: string; }'.

  8 callback({
             ~
  9     rows: [
    ~~~~~~~~~~~
...
 11     ]
    ~~~~~
 12 });
...

Removing | undefined from the parameter type makes the error be the same as in 5.8

Metadata

Metadata

Assignees

Labels

Domain: Type CheckingRelated to type checking, grammar checkingbugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions