【问题标题】:Typescript working with Modules使用模块的打字稿
【发布时间】:2016-06-03 05:42:25
【问题描述】:

我正在尝试构建我的第一个简单的基于 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我做错了什么

【问题讨论】:

    标签: javascript node.js module typescript


    【解决方案1】:
    // 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 中看到它们

    【讨论】:

      猜你喜欢
      • 2023-03-29
      • 2013-08-27
      • 1970-01-01
      • 2017-11-05
      • 2016-06-22
      • 2016-05-13
      • 2014-09-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多