19
Vote

Make class / variable declarations open ended

description

Make class declarations open ended.

e.g. the following should be valid
declare class Foo  {
    static bar: any;
    bas():any;
}

// Amend Foo
declare class Foo  {
    static anotherStatic: any;
    anotherMember():any;
}

Workaround for Static properties

Warning : not flexible, for demo only. Use the fact that modules are merged with classes in the type declaration space e.g.
declare class Foo  {
    static bar: any;
    bas():any;
}

// Amend Foo
declare module Foo  {
    export var anotherStatic: any;
}

Workaround for Member properties

Unfortunately this requires rewriting the original definition:
declare var Foo : FooStatic;
interface FooStatic { // Static properties
    new (): Foo;
    bar: any;
}
interface Foo{ // Instance properties
    bas():any;
}

// Amend Foo 
interface FooStatic  {
   anotherStatic: any;
}
interface Foo{
    anotherMember():any;
}
This means the user needs to create three things (var, static interface, instance interface) and is extremely intuitive to the new (average) TypeScript user (people that don't make definitions).

comments

billti wrote Apr 18, 2013 at 5:52 PM

I believe language changes coming in the 0.9.0 release should address this, but assigning to Jonathan to track.

basarat wrote May 29, 2013 at 9:59 PM

Just wanted to mention it is not working for TS 0.9.0-alpha at the moment. e.g. this user wants to add a static member to Object http://stackoverflow.com/q/16816414/390330

activwerx wrote May 30, 2013 at 11:32 AM

Thanks for addressing my StackOverflow question here. I have posted here WorkItem 1085 to illustrate further concerns with the current implementation for ambient declarations.

basarat wrote Jun 19, 2013 at 9:53 PM

Missing in the relased TS 0.9 : http://stackoverflow.com/q/17201358/390330

basarat wrote Aug 25, 2013 at 9:31 AM

Just a mention, the changes that came with TS 0.9.1.1 either have a bug or haven't addressed this. e.g. the following gives a duplicate identifier error :
module RegExp{
    export function quote(whatev){return whatev;}
}

RegExp.quote('foo');
See:
http://stackoverflow.com/a/18427281/390330

manast wrote Nov 17, 2013 at 12:30 PM

It would be interesting to know if there is any theoretical or practical reason why Classes are not open ended? This is quite a show stopper for us used to code in vanilla javascript, where extending existing classes is many times really useful and a powerful feature that javascript provides but other languages are lacking...

jamesnw wrote Mar 25 at 9:19 AM

I agree - I keep wanting to do this at times.

basarat wrote Apr 7 at 10:17 AM

I've updated to take into account the language changes in TS 0.9 which are still insufficient for the general purpose.