Interfaces that contain classes as properties

Topics: General
Oct 7, 2012 at 8:47 PM
Edited Oct 7, 2012 at 8:48 PM

Hi! I'm trying to generate a declaration file for the YUI Library and run into something I could use some help figuring out.

YUI works like this. There's a global YUI() function which returns an instance of the YUI class, similar to what jQuery does. I can simulate it by declaring:

import YUI3 = module('YUI');
declare var YUI: YUI3.YUI;

var Y = YUI();

So far so good. Now the Y instance also contains constructors that can be invoked with "new", and I can't figure out how to let VS/TypeScript know that  this should work:

var Y = YUI();
var widget = new Y.Widget();

Any ideas?


Oct 7, 2012 at 9:30 PM

Try declaring your YUI class as a module:

declare module YUI_Class {
	class Widget {
declare function YUI(): YUI_Class;

var y = YUI();
var widget = new y.Widget();


Oct 7, 2012 at 9:33 PM

This works too:

interface YUI_Widget {

interface YUI_Class {
	Widget: new () => YUI_Widget;
declare function YUI(): YUI_Class;

var y = YUI();
var widget = new y.Widget();;
Oct 7, 2012 at 9:42 PM

That last one is exactly what I needed. Thanks!

Oct 7, 2012 at 10:09 PM
Edited Oct 7, 2012 at 10:09 PM

Well, it got a lot more complicated, but I finally understood how I'm supposed to declare all this.

module Y {

    interface Widget {
        render(): Widget;

    interface WidgetStatic {
        new (config?: Object): Widget;

        getByNode(node: HTMLElement): Widget;

    interface YUI {
        use(modules: string[], fn: (Y: YUI) => any): YUI;

        Widget: WidgetStatic;

    interface YUIStatic {
        (config?: Object): YUI;

        add(name: string, factory: (Y: YUIStatic) => any , version?: string, options?: Object);


declare var YUI: Y.YUIStatic;

Thanks again!

Oct 7, 2012 at 10:25 PM

There are probably 2 or 3 ways you could define it and yes... It can't get complicated in a hurry :)