Skip to content

Generic constraint not playing well with new 'object' type. #15920

Closed

Description

TypeScript Version: Version 2.4.0-dev.20170517

In the code below, I am am uncertain if this is a bug or a misunderstanding on my part to do with the new, more restrictive, 'object' type.

Code

// The following produces a compiler error:
//   Type 'A & B' does not satisfy the constraint 'Base'.
//   Type 'A & B' is not assignable to type 'object'.
//   (type parameter) B in foo<A extends Base, B extends Base>(): Foo<Base, A & B>
//
type Base = boolean|object;

type Foo<DOM, CODOM extends DOM> = (dom: DOM) => CODOM;

function foo<A extends Base, B extends Base>(): Foo<Base, A & B>    // <= error at A & B
{
  return <any> function(x: Base) { return x; };
}


// Removing the constraint on CODOM in type Foo2 and
// all is well.
//
type Base2 = boolean|object;

type Foo2<DOM, CODOM> = (dom: DOM) => CODOM;    // <= "fixed" by removing extends DOM

function foo2<A extends Base2, B extends Base2>(): Foo2<Base2, A & B>
{
  return <any> function(x: Base2) { return x; };
}


// All can also be made well by changing object to Object in
// type Base3.
//
type Base3 = boolean|Object;  // <= also "fixed" by resorting to 'Object' instead of 'object'

type Foo3<DOM, CODOM extends DOM> = (dom: DOM) => CODOM;

function foo3<A extends Base3, B extends Base3>(): Foo3<Base3, A & B>
{
  return <any> function(x: Base2) { return x; };
}

Activity

  1. ahejlsberg commented on May 17, 2017

    @ahejlsberg
    Member

    A simpler repro:

    function f<A extends object | boolean, B extends object | boolean>(x: A & B) {
        let z: object | boolean = x;  // Error here
    }

    The issue relates to how we break down union and intersection types in type relationship checking. We always first break down union types, i.e. we check that A & B is assignable to object or boolean. Neither of those are true (because, for example, A could be a boolean which isn't assignable to object). Effectively, when checking the constraints, we end up with union types sitting below an intersection type, which produces wrong results. In order to solve this we would have to "lift" the constraints up and distribute the & operator over any | operators in the constraints. It's doable, but not trivial.

  2. ahejlsberg commented on May 17, 2017

    @ahejlsberg
    Member

    See #11717 for context on union/intersection normalization.

  3. added
    Design LimitationConstraints of the existing architecture prevent this from being fixed
    and removed
    BugA bug in TypeScript
    on Jul 23, 2018
  4. typescript-bot commented on Aug 1, 2018

    @typescript-bot
    Contributor

    Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions