镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Seek most-overlapping types when relating object types in unions #26450

Description

From Rado Kirov (@rkirov) at DefinitelyTyped/DefinitelyTyped#28044 (comment)

interface A {
    a: string;
    b: number
};

interface Other {
    o: number;
}

declare function f(x: A | Other): any;

export let x = { a: '', b: '' };
export let y = f(x);

Expected

Argument of type '{ a: string; b: string; }' is not assignable to parameter of type 'Other | A'.
  Type '{ a: string; b: string; }' is not assignable to type 'A'.
    Types of property 'b' are incompatible.
      Type 'string' is not assignable to type 'number'.

Actual

Argument of type '{ a: string; b: string; }' is not assignable to parameter of type 'A | Other'.
  Type '{ a: string; b: string; }' is not assignable to type 'Other'.
    Property 'o' is missing in type '{ a: string; b: string; }'.

Activity

  1. bowenni commented on Aug 14, 2018

    @bowenni

    I'm not sure if this is a valid ask. Would it be possible to use the most-overlapping declaration in error message for overloads too?

    declare function f(a: string, b: string, c: string): void;
    declare function f(a: () => string, b: () => string, c: () => string): void;
    f('1', '1', {});

    Actual
    ERROR(3,3): : Argument of type '"1"' is not assignable to parameter of type '() => string'.
    Expected
    ERROR(3,13): : Argument of type '{}' is not assignable to parameter of type 'string'.

  2. DanielRosenwasser commented on Aug 15, 2018

    @DanielRosenwasser
    MemberAuthor

    Bowen Ni (@bowenni) I think that is a UX improvement we want to see, but I'd rather we file a different issue there. Overload resolution is pretty complicated and would warrant its own work item.

  3. CyrusNajmabadi commented on Aug 30, 2018

    @CyrusNajmabadi
    Contributor

    A vote on this over from Pulumi land. We have extensive use of union types, and errors here can be painful :)

  4. KaelWD commented on Sep 6, 2018

    @KaelWD

    Similar problem with Vue.

    Code:

    Vue.extend({
      props: {
        value: null
      }
    })

    3.0.1:

    Argument of type '{ props: { value: null; }; }' is not assignable to parameter of type 'ComponentOptions<Vue, DefaultData<Vue>, DefaultMethods<Vue>, DefaultComputed, PropsDefinition<Record<string, any>>, Record<string, any>>'.
      Types of property 'props' are incompatible.
        Type '{ value: null; }' is not assignable to type 'string[] | RecordPropsDefinition<Record<string, any>> | undefined'.
          Type '{ value: null; }' is not assignable to type 'undefined'.
    

    2.9.2:

    Argument of type '{ props: { value: null; }; }' is not assignable to parameter of type 'ComponentOptions<Vue, DefaultData<Vue>, DefaultMethods<Vue>, DefaultComputed, PropsDefinition<Rec...'.
      Type '{ props: { value: null; }; }' is not assignable to type 'ComponentOptions<Vue, DefaultData<Vue>, DefaultMethods<Vue>, DefaultComputed, PropsDefinition<Rec...'.
        Types of property 'props' are incompatible.
          Type '{ value: null; }' is not assignable to type 'string[] | RecordPropsDefinition<Record<string, any>> | undefined'.
            Type '{ value: null; }' is not assignable to type 'RecordPropsDefinition<Record<string, any>>'.
              Property 'value' is incompatible with index signature.
                Type 'null' is not assignable to type 'PropValidator<any>'.
    
  5. DanielRosenwasser commented on Sep 14, 2018

    @DanielRosenwasser
    MemberAuthor

    Doesn't "dive in" to the objects like Joe Duffy (@joeduffy) and Cyrus Najmabadi (@CyrusNajmabadi) were hoping for, but a fix is up on #27087. Maybe Wesley Wigham (@weswigham) has some ideas here.

  6. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugA bug in TypeScriptDomain: Error MessagesThe issue relates to error messagingFixedA PR has been merged for this issue

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions