Skip to content

Error from incorrect Partial of union on different node in TS vs Go #1050

Open
@benjaminjkraft

Description

@benjaminjkraft

This code is kinda cursed and there are many ways for us to fix it, but it errors slightly differently in TS vs. Go so I figured I'd report it in case it's unexpected.

type T = { type: "a"; a?: { k1: "v1"; k2: "v2" } } | { type: "b" };

function f(args?: Partial<T>) {}

export function g() {
  f({ a: { k2: "v2" } });
}

In TS we get:

src/partial.ts:6:7 - error TS2741: Property 'k1' is missing in type '{ k2: "v2"; }' but required in type '{ k1: "v1"; k2: "v2"; }'.

6   f({ a: { k2: "v2" } });
        ~

  src/partial.ts:1:29
    1 type T = { type: "a"; a?: { k1: "v1"; k2: "v2" } } | { type: "b" };
                                  ~~
    'k1' is declared here.

In Go we get:

src/partial.ts:6:5 - error TS2345: Argument of type '{ a: { k2: "v2"; }; }' is not assignable to parameter of type 'Partial<T> | undefined'.
  Types of property 'a' are incompatible.
    Property 'k1' is missing in type '{ k2: "v2"; }' but required in type '{ k1: "v1"; k2: "v2"; }'.

6   f({ a: { k2: "v2" } });
      ~~~~~~~~~~~~~~~~~~~

  src/partial.ts:1:29 - 'k1' is declared here.
    1 type T = { type: "a"; a?: { k1: "v1"; k2: "v2" } } | { type: "b" };
                                  ~~

We noticed because in the original source the object is spread across a few lines, and currently it has @ts-expect-error which would then need to move to a different line. Plus in the real case it's better replaced by a cast anyway (or DeepPartial). So feel free to ignore if not useful!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Domain: Type CheckingRelated to type checking, grammar checking

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions