Javascript 使用模块的Typescript

Javascript 使用模块的Typescript,javascript,node.js,module,typescript,Javascript,Node.js,Module,Typescript,我正在尝试构建我的第一个简单的基于ORM的NodeJS应用程序 我像这样创建了数据库模块 import * as Knex from 'knex'; import * as Bookshelf from 'bookshelf'; module Database { class Config { private static _knex: Knex = Knex({ client: 'mysql', connection:

我正在尝试构建我的第一个简单的基于ORM的NodeJS应用程序

我像这样创建了
数据库
模块

import * as Knex from 'knex';
import * as Bookshelf from 'bookshelf';

module Database {
    class Config {
        private static _knex: Knex = Knex({
            client: 'mysql',
            connection: {
                host: '127.0.0.1',
                user: 'root',
                password: '',
                database: 'test',
                charset: 'utf8'
            }
        });

        static _bookshelf: Bookshelf = Bookshelf(Config._knex);
    }

    export function bookshelf() {
        Config._bookshelf.plugin('registry');
        Config._bookshelf.plugin(['virtuals']);
        return Config._bookshelf;
    }
}
我试图在一个
DAO
类中使用它

/// <reference path="../models/usermodel.ts" />
/// <reference path="../network/database.ts" />
module DAO {
    export class UserDAO {
        create(user: Model.User): Model.User { //Model.User is imported nicely
            var test = Database.bookshelf(); //what's wrong with this
            return null;
        }
    }
}
//
/// 
模块DAO{
导出类UserDAO{
创建(user:Model.user):Model.user{//Model.user被很好地导入
var test=Database.bookshelf();//这有什么问题吗
返回null;
}
}
}
最后出现以下错误
dao/userdao.ts(18,24):错误TS2304:找不到名称“数据库”。

这是我第一次学习
类型脚本和模块
,如果我做错了,请告诉我

更新: 只要我在database.ts中添加
import语句,它就不起作用/找不到name。使用
import*作为来自某个

//database.ts的内容,我做错了什么
// database.ts
/// <reference path="<pathToKnexDefinetelyTypedFile>" />
// if you don't already have knex.d.ts
// https://raw.githubusercontent.com/DefinitelyTyped/DefinitelyTyped/master/knex/knex.d.ts
/// <reference path="<pathToBookshelfDefinetelyTypedFile>" />
// if you don't already have bookshelf.d.ts
// https://raw.githubusercontent.com/DefinitelyTyped/DefinitelyTyped/master/bookshelf/bookshelf.d.ts

// normally these references are unnecessary, but you have 
// to download all the ts for third libraries 
// you normally place them in a 'typings' folder 
// or choose another name for the folder, irrelevant,
// then the IDE should recognize them (ts files) easily.

import * as Knex from 'knex';
import * as Bookshelf from 'bookshelf';

module Database {
    class Config {
        private static _knex: Knex = Knex({
            client: 'mysql',
            connection: {
                host: '127.0.0.1',
                user: 'root',
                password: '',
                database: 'test',
                charset: 'utf8'
            }
        });

        static _bookshelf: Bookshelf = Bookshelf(Config._knex);
    }

    export function bookshelf() {
        Config._bookshelf.plugin('registry');
        Config._bookshelf.plugin(['virtuals']);
        return Config._bookshelf;
    }
}

// Don't forget the export, that why you are getting that error
export { Database }

// dao.ts
/// <reference path="../models/usermodel.ts" />
/// <reference path="../network/database.ts" />
import { Database } from './database';
module DAO {
    export class UserDAO {
        create(user: Model.User): Model.User { //Model.User is imported nicely
            var test = Database.bookshelf(); 
            // what's wrong with this ? 
            // Maybe the export and the import you forgot to add 
            return null;
        }
    }
}
/// //如果你还没有knex.d.ts // https://raw.githubusercontent.com/DefinitelyTyped/DefinitelyTyped/master/knex/knex.d.ts /// //如果你还没有书架F.d.t // https://raw.githubusercontent.com/DefinitelyTyped/DefinitelyTyped/master/bookshelf/bookshelf.d.ts //通常这些参考是不必要的,但你有 //下载第三个库的所有ts //您通常将它们放在“打字”文件夹中 //或者为文件夹选择另一个名称,不相关, //然后IDE应该很容易识别它们(ts文件)。 从“Knex”导入*作为Knex; 从“Bookshelf”导入*作为书架; 模块数据库{ 类配置{ 私有静态_knex:knex=knex({ 客户端:“mysql”, 连接:{ 主持人:“127.0.0.1”, 用户:'根', 密码:“”, 数据库:“测试”, 字符集:“utf8” } }); 静态书架:书架=书架(配置); } 导出功能书架(){ Config._bookshelf.plugin('registry'); Config._bookshelf.plugin(['virtuals']); 返回配置。\u书架; } } //不要忘记导出,这就是为什么会出现错误 导出{数据库} //道茨 /// /// 从“./Database”导入{Database}; 模块DAO{ 导出类UserDAO{ 创建(user:Model.user):Model.user{//Model.user被很好地导入 var test=Database.bookshelf(); //这个怎么了? //可能是您忘记添加的导出和导入 返回null; } } }
引用注释仅由typescript使用,它们不会被传输,您不会在生成的js中看到它们。如果IDE能够识别项目中的所有ts文件,则不需要引用注释

您必须导入/导出在当前文件中使用的名称空间/模块,这是因为导入/导出是传输的,它们将在js中编译,您将在生成的js中看到它们