0
votes

I'm trying to write a Node module with TypeScript, this is how far I got:

MysqlMapper.ts

export class MysqlMapper{

    private _config: Mysql.IConnectionConfig;

    private openConnection(): Mysql.IConnection{
        ...
    }

    private createSelectAllQuery(constructor): string{
    ...
    }

    public getAll<T>(constructor): Q.Promise<Array<T>>{
    .....
    }
    constructor(config: Mysql.IConnectionConfig){
    ... 
    }

}

index.ts

export * from "./MysqlMapper";

package.json

{
  "name": "mysql-metadata-orm",
  "version": "1.0.0",
  "description": "A Simple ORM using reflect-metadata",
  "main": "build/index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "..."
  },
  "author": "+",
  "license": "MIT",
  "dependencies": {
    "mysql": "^2.9.0",
    "q": "^1.4.1",
    "reflect-metadata": "^0.1.2"
  }
}

I activated the declaration-generation in my tsconfig-file, but I really don't understand how to get a declaration file which allows to import the module like this:

import * as Mapper from "mysql-metadata-mapper"

I hope someone can help me with this, because it drives me very crazy.

@Update

These are the generated .d.ts-Files:

index.d.ts

export * from "./MysqlMapper";

MysqlMapper.d.ts

import * as Q from "q";
import * as Mysql from "mysql";
export declare class MysqlMapper {
    private _config;
    private openConnection();
    private createSelectAllQuery(constructor);
    getAll<T>(constructor: any): Q.Promise<Array<T>>;
    constructor(config: Mysql.IConnectionConfig);
}

How do I get the declaration file?

1
Can you maybe post here what the auto generated declarations produce for this module? - FlorianTopf
I know this is a pretty old question, but for anyone else stumbling across it later (as I did this past week), you might find this typescript boilerplate project helpful: github.com/bitjson/typescript-starter - Jason Dreyzehner

1 Answers

0
votes

I'd suggest that you make your own declaration file: (e.g. globals.d.ts)

declare module 'mysql-metadata-mapper' {
    import * as mapper from './MysqlMapper';
    export = mapper;
}

then you can reference this declaration file e.g. /// <reference path="../globals.d.ts" />

and import the module as you like with:

import * as Mapper from 'mysql-metadata-mapper'

I'm not 100% sure if this is the cleanest solution, but at least it should work like you want it.