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

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

Closed
ghost opened this issue May 17, 2017 · 3 comments
Closed

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

ghost opened this issue May 17, 2017 · 3 comments
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@ghost
Copy link

ghost commented May 17, 2017

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; };
}
@mhegazy mhegazy added the Bug A bug in TypeScript label May 17, 2017
@ahejlsberg
Copy link
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.

@ahejlsberg
Copy link
Member

See #11717 for context on union/intersection normalization.

@mhegazy mhegazy added Design Limitation Constraints of the existing architecture prevent this from being fixed and removed Bug A bug in TypeScript labels Jul 23, 2018
@typescript-bot
Copy link
Collaborator

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
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed
Projects
None yet
Development

No branches or pull requests

3 participants