2

I would like to create a typescript definition for a big JavaScript class without adding all its members, I would like it to be of type "any". For example:

ContainerSurface.d.ts:

declare class ContainerSurface {
}
export = ContainerSurface;

And just use the class and call any members on it without having them "declared", like:

MyClass.ts:

import ContainerSurface = require('ContainerSurface');

class MyClass extends ContainerSurface {
    constructor(options) {
        super(options);
        var a: ContainerSurface({option: "test"});
        a.add("test");
    }
}

export = MyClass;
2
  • Why have a class if you're not going to give it an interface? This pretty much goes against what TypeScript is all about. Commented Feb 6, 2015 at 18:14
  • If you want to use a javascript library (in our case famo.us), but don't have the time and knowledge to generate .d.ts files for it, and need to inherit using typescript from a javascript class you want this... believe me... Commented Feb 6, 2015 at 22:01

1 Answer 1

2

You can achieve this using the following:

declare var ContainerSurface: any;

export = ContainerSurface;

This is the first step in my Definition Files Made Easy process - so you can gradually add types over time using this as the starting point.

Step two is to loosely specify properties and methods:

declare class ContainerSurface {
    myMethod: any;
    myProperty: any;
}

export = ContainerSurface;

Grab any quick wins, like primitive types, and just add the stuff you actually use to start off with.

Sign up to request clarification or add additional context in comments.

4 Comments

This didn't work for me... I'm using AMD modules, could it be that this is the problem? It works for me the way I described, but then I must include all functions I call. Using your approach I get the error: "super can only be referenced in a derived class', 'extends of exported class '...' has or is using private name '...'.
You can't extend any. You'll have to supply more type information if you want to use inheritance, but you can do it gradually as per the article I linked.
I did this in my current code base, I declared: declare class ContainerSurface { prototype(opts:any); add(s:string); } export = ContainerSurface; But what if there are a whole set of other functions, but that I don't want to declare them (for example del(), prev(), next()), is there a way to do this, or should I declare all functions that I actually use?
Each property you need to access can be declared on the class as any until you are ready to add more precise definitions. i.e. myMethod : any;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.