Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
DanielRosenwasser opened this issue Aug 14, 2018 · 5 comments
Closed
Assignees
Labels
Bug A bug in TypeScript Domain: Error Messages The issue relates to error messaging Fixed A PR has been merged for this issue

Comments

@DanielRosenwasser
Copy link
Member

From @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; }'.
@bowenni
Copy link

bowenni commented Aug 14, 2018

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'.

@DanielRosenwasser
Copy link
Member Author

@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.

@CyrusNajmabadi
Copy link
Contributor

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

@KaelWD
Copy link

KaelWD commented Sep 6, 2018

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>'.

@DanielRosenwasser
Copy link
Member Author

DanielRosenwasser commented Sep 14, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Domain: Error Messages The issue relates to error messaging Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

6 participants