使用模块的打字稿

时间:2016-06-03 05:42:26

标签: javascript node.js module typescript

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

我像这样创建了Database模块

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.ts(18,24): error TS2304: Cannot find name 'Database'.

这是我第一次学习Typescript and Modules,如果我做错了,请告诉我。

更新 一旦我添加import statements in database.ts,它就无法正常工作/无法找到名称。我使用import * as something from some

做错了什么

1 个答案:

答案 0 :(得分:2)

// 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;
        }
    }
}

参考评论仅用于打字稿,它们不会被翻译,你不会在生成的js中看到它们。如果IDE识别出项目中的所有ts文件,则不需要引用注释。

您必须导入/导出您在当前文件中使用的命名空间/模块,因为导入/导出已被转换,它们将以js编译,您将在生成的js中看到它们