TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

Error TS2506 when class extends class in module with same name

Open cascadian opened this issue 11 years ago • 10 comments

I am using some type definitions that follow the pattern below and that have been compiling up until commit d71af8a3fb485880866e097cbb6963d5aab62678

declare module MyModule {
  class Foo extends Foo.__Class {}
  module Foo {
    class __Class {
      constructor(x: number, y: number);
    }
  }
}

After commit d71af8a3fb485880866e097cbb6963d5aab62678 I'm getting errors like error TS2506: 'Foo' is referenced directly or indirectly in its own base expression.

cascadian avatar Jun 22 '15 21:06 cascadian

Can you point us to some examples? We'd like to understand why someone would write that

RyanCavanaugh avatar Jun 22 '15 22:06 RyanCavanaugh

Lots of examples under https://github.com/fivetran/typescript-closure-tools

Such as https://github.com/fivetran/typescript-closure-tools/blob/master/index/closure-library/closure/goog/async/delay.d.ts

In my project, I'm using the typescript definitions from https://github.com/3x0dv5/ol3-typescript-definitions and the author indicates they were created with the help of typescript-closure-tools

cascadian avatar Jun 22 '15 23:06 cascadian

I can't think of a good way to fix this. We'd have to somehow special case property access to first check if a property can be found without looking at the type of the left hand side (aka using symbols instead). This is not a regression, so I'm going to say won't fix. I recommend instead writing the code like this:

declare module MyModule {
    class Base {
        // everything you want Foo and Foo.__Class to have in common
    }
    class Foo extends Base { }
    module Foo {
        class __Class extends Base {
            constructor(x: number, y: number);
        }
    }
}

JsonFreeman avatar Jul 07 '15 00:07 JsonFreeman

I'll add that the spec says you cannot reference yourself in your base reference. And referencing the module is still referencing yourself.

JsonFreeman avatar Jul 07 '15 00:07 JsonFreeman

Oh, you can also write it like this if you want:

declare module MyModule {
    class Foo { }
    module Foo {
        class __Class extends Foo {
            constructor(x: number, y: number);
        }
    }
}

Although this has the downside that __Class inherits a static member called __Class.

JsonFreeman avatar Jul 07 '15 00:07 JsonFreeman

I would like to explain the reason typescript-closure-tools generates these strange looking classes. In the Google Closure class model, static members are not inherited, and the closure library frequently overrides static members with incompatible overloads. The solution is to create a parallel hierarchy RealClass.__Class which has no static members, and then have RealClass extends RealClass.__Class.

It's not actually important that __Class be a member of its own "real" implementation -- it was just a convenient place to stash it.

georgewfraser avatar Jul 10 '15 17:07 georgewfraser

@georgewfraser Can you give an example where if you don't do this pattern, you would end up inheriting static members? I'm not clear on which part of the pattern prevents the inheritance.

JsonFreeman avatar Jul 10 '15 18:07 JsonFreeman

Closure classes often contain dictionaries that sometimes get smaller in subclasses:

declare class Super {
    static dict:{
        a: number;
        b: number;
    }
}

// error TS2417: Class static side 'typeof Sub' incorrectly extends base class static side 'typeof Super'.
declare class Sub extends Super {
    static dict: {
        a: number;
    }
}

This pattern also fails, which is more surprising:

declare class Super {
}

declare module Super {
    var dict: {
        a: number;
        b: number;
    };
}

// error TS2417: Class static side 'typeof Sub' incorrectly extends base class static side 'typeof Super'.
declare class Sub extends Super {
}

declare module Sub {
    var dict: {
        a: number;
    }
}

The fix is to create a parallel hierarchy without the static member:

declare class Super extends Super__Class {
    static dict:{
        a: number;
        b: number;
    }
}

declare class Super__Class {
}

declare class Sub extends Sub__Class {
    static dict: {
        a: number;
    }
}

declare class Sub__Class extends Super__Class {
}

We've already changed to the above pattern (Super__Class instead of Super.__Class) so this is working now.

georgewfraser avatar Jul 11 '15 01:07 georgewfraser

Oh I get it now! Thanks for explaining. And I'm glad changing to the other pattern works.

JsonFreeman avatar Jul 13 '15 04:07 JsonFreeman

A class named Foo is not allowed to reference Bar in a namespace named Foo. Another workaround is to put Bar in any other namespace and also in the Foo namespace.

declare module MyModule {
  class Foo extends __Foo.Bar {}

  module __Foo {
    class Bar {
      constructor(x: number);
      bar();
    }
  }
  module Foo {
      class Bar extends __Foo.Bar {}
  }
}

MyModule.Foo.Bar;
new MyModule.Foo(1);

This workaround creates a lot of duplication. Is there another way?

module Foo extends module __Foo lol!

If this is the only way to preserve namespaces for libraries like YUI and OpenLayers2 then this should be fixed.

ca0v avatar Oct 14 '15 11:10 ca0v