Skip to content

Better display representation for intersection types #7705

Description

@louy

I'm not sure if this has been discussed before, but join types are mostly unreadable. I think the type should be prettified somehow before being displayed to the user.

TypeScript Version:

1.8

Code

type T1 = {};
type T2 = {test: number};
type J = T1 & T2;

Expected behavior:

var x: J;
typeof x; // {test: number}

Actual behavior:

var x: J;
typeof x; // {} & {test: number}

Activity

  1. changed the title [-]Better join types display[/-] [+]Better intersection display for intersection types[/+] on Apr 6, 2016
  2. changed the title [-]Better intersection display for intersection types[/-] [+]Better display representation for intersection types[/+] on Apr 6, 2016
  3. JabX commented on Apr 13, 2016

    @JabX

    👍
    I use intersections a lot and I would love to have this feature.

  4. mhegazy commented on Apr 13, 2016

    @mhegazy
    Contributor

    so just to be clear is the request 1. to remove the empty object type if exists, or 2. to collapse all types. i.e.:

    type T = { } & { a: number } & { b: string };
    
    var x: T; // What is x? 
                // 1. { a: number } & { b: string }
                // 2. { a: number, b: string }
  5. louy commented on Apr 13, 2016

    @louy
    Author

    The second actually.

  6. mhegazy commented on Apr 13, 2016

    @mhegazy
    Contributor

    ok one more clarification, i assume this only applies to anonymous types. so 2.a would be the desired behavior.

    class C { c: boolean; }
    
    type T = C & { a: number } & { b: string };
    
    var x: T; // What is x? 
                // 2.a C & { a: number; b: string }
                // 2.b { a: number, b: string; c: boolean }
  7. JabX commented on Apr 14, 2016

    @JabX

    I suppose it's better to limit this to anonymous types, or else it could rapidly go out of control with "big" named types, and you'll lose some type information.

  8. zpdDG4gta8XKpMCd commented on Apr 22, 2016

    @zpdDG4gta8XKpMCd

    related #6070

  9. added this to the milestone on May 9, 2016
  10. RyanCavanaugh commented on May 9, 2016

    @RyanCavanaugh
    Member

    Accepting PRs, with a big caveat. This is actually quite a bit trickier than it appears because of how the compiler handles anonymous self-referential types. A PR here should be accompanied by a large test suite of recursively self-referential types to ensure there aren't any lingering runaway recursion bugs.

    See also comments in #8228.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions