Bennett
Bennett

Reputation: 487

TypeScript 1.4 CommonJS modules exposed as constructors

This question is very close to my question, and the comments in the linked question are really helpful, but I cannot seem to make a pure "ghost" module.

The code I'm struggling with can be found here I have a file "mariasql.d.ts", and a test file "mariasql-tests.ts". The *.d.ts file exposes a constructor function, that works like so:

var Client = require('mariasql),
c = new Client();

Based on the other SO quesion, the *.d.ts file exports like so:

module MARIASQL {
    //... edited for brevity
    export interface MariaClient {
        connect(config:ClientConfig):void;
        end():void;
        destroy():void;
        escape(query:string):string;
        query(q:string, placeHolders?:Dictionary, useArray?:boolean):MariaQuery;
        query(q:string, placeHolders?:Array<any>, useArray?:boolean):MariaQuery;
        query(q:string, useArray?:boolean):MariaQuery;
        prepare(query:string): MariaPreparedQuery;
        isMariaDB():boolean;
        on(signal:string, cb:MariaCallBackError): MariaClient;    // signal 'error'
        on(signal:string, cb:MariaCallBackObject): MariaClient;   // signal 'close'
        on(signal:string, cb:MariaCallBackVoid): MariaClient;     // signal 'connect'
        connected: boolean;
        threadId: string;
    }

    export interface Client {
        new ():MariaClient;
        ():MariaClient;
        prototype: MariaClient;
    }
}

declare module "mariasql" {
    var Client:MARIASQL.Client;
    export = Client;
}

The import code in the test file looks like so:

/// <reference path="../node/node-0.10.d.ts" />
/// <reference path="./mariasql.d.ts" />

// Example 1 - SHOW DATABASES
import util = require('util');
import Client = require('mariasql');

var c:Client = new Client(),
    inspect = util.inspect;
// edited ...

note - the github link will read c:Client.prototype which also did not work.

I feel like I must be misunderstanding something(s) obvious.

Upvotes: 1

Views: 158

Answers (1)

basarat
basarat

Reputation: 275857

What you have is perfectly fine. The only minor fix needed is in your test instead of

var c:Client.prototype = new Client(),

You need to use the interface:

var c: MARIASQL.MariaClient = new Client(),

Or remove it entirely and let the compiler infer it for you ;).

Upvotes: 2

Related Questions