Skip to content

Mixin pattern issues when "declaration:true" is used - tsc cannot infer class def properly #14597

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

Closed
Hotell opened this issue Mar 11, 2017 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@Hotell
Copy link

Hotell commented Mar 11, 2017

TypeScript Version: 2.2.1

Code

Demo you have to download and run it on your machine with declaration: true

// A *self-contained* demonstration of the problem follows...
export class Point {
    constructor(public x: number, public y: number) {}
}

export class Person {
    constructor(public name: string) {}
}

export type Constructor<T> = new (...args: any[]) => T;

export type TaggedProps = {
    tag: string,
    isTagged(): boolean,
}

export function Tagged<T extends Constructor<{}>>(Base: T): Constructor<TaggedProps> & T {
    return class TaggedMixin extends Base {
        tag: string;
        constructor(...args: any[]) {
          super(...args);
          this.tag = '';
        }
        isTagged() { return Boolean(this.tag.length); }
    }
}

export const TaggedPoint = Tagged(Point);

const point = new TaggedPoint(10, 20);
point.tag = 'hello';
point.isTagged();
console.log(point.x);

export class Customer extends Tagged(Person) {
    accountBalance: number;

    onFoo() {
      return this.isTagged() ? this.tag : 'ups';
    }
}

const customer = new Customer('Joe');
customer.tag = 'test';
customer.accountBalance = 0;
customer.onFoo();

Expected behavior:
No errors when using declaration:true and proper type inference of returned mixin class from function

Actual behavior:
Provided code works without errors, but when we want generate type definitions via declaration:true tsc will error out.

Problem is that TSC is not able to infer the returned class type definition when generating ambient definitions ( so that's why I explicitly provide return type export function Tagged<T extends Constructor<{}>>(Base: T): Constructor<TaggedProps> & T { )

Although that is not the exact type that TSC inferes when declaration is not turned on:

Infered type definition:

function Tagged<T extends Constructor<{}>>(Base: T): { 
  new (...args: any[]): TaggedMixin; 
  prototype: Tagged<any>.TaggedMixin; 
} & T

screen shot 2017-03-11 at 6 02 37 pm

@RyanCavanaugh
Copy link
Member

The issue is that .d.ts files can't contain expression code, and the extends operand Tagged(Person) is an expression rather than an identifier which points to a type+value.

@Hotell
Copy link
Author

Hotell commented Mar 12, 2017

Right, are there any plans to improve this in the future?

For anyone else the fix is simple enough ( although it took me quite a lot time to figure it out :D )

++const TaggedPerson = Tagged(Person);
--export class Customer extends Tagged(Person) {
++export class Customer extends TaggedPerson {
    accountBalance: number;

    onFoo() {
      return this.isTagged() ? this.tag : 'ups';
    }
}

also fixed here wc-catalogue/blaze-elements@d5d435e

@DanielRosenwasser DanielRosenwasser added Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. Suggestion An idea for TypeScript and removed Design Limitation Constraints of the existing architecture prevent this from being fixed labels Mar 12, 2017
@mhegazy
Copy link
Contributor

mhegazy commented Mar 13, 2017

This is tracked by #14075, either allow some intersection type in extends clause of classes in ambient context, or add the const as suggested by @Hotell.

@mhegazy mhegazy added Duplicate An existing issue was already created and removed Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. Suggestion An idea for TypeScript labels Mar 13, 2017
@Hotell
Copy link
Author

Hotell commented Mar 13, 2017

Right closing in favor of #14209 and #14075

Thanks guys!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants