使用TypeScript创建NodeJS模块

时间:2022-06-07 17:00:49

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

我正在尝试用TypeScript编写一个Node模块,这是我得到了多远:

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:

我在tsconfig文件中激活了声明生成,但我真的不明白如何获取一个允许导入模块的声明文件:

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:

这些是生成的.d.ts文件:

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 个解决方案

#1


0  

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

我建议你自己制作声明文件:(例如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'

从'mysql-metadata-mapper'导入*作为Mapper

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

我不是100%确定这是否是最干净的解决方案,但至少它应该像你想要的那样工作。

#1


0  

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

我建议你自己制作声明文件:(例如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'

从'mysql-metadata-mapper'导入*作为Mapper

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

我不是100%确定这是否是最干净的解决方案,但至少它应该像你想要的那样工作。

相关文章