2016-06-03 2 views
1

첫 번째 간단한 ORM 기반 노드 JS 응용 프로그램을 빌드하려고합니다.모듈로 작업하는 Typescript

나는이

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

같은 Database 모듈을 생성 그리고 dao/userdao.ts(18,24): error TS2304: Cannot find name 'Database'.

그것은이다이 오류와 함께 종료됩니다 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; 
     } 
    } 
} 

중 하나를 사용하는 것을 시도하고있다 Typescript and Modules에 대한 나의 첫번째 학습, 내가 뭔가 잘못하고 있는지 알려주세요.

업데이트 : 가 최대한 빨리 import statements in database.ts를 추가, 그것은 작동하지 않습니다/이름을 찾을 수 없습니다. 내가 참조 코멘트 만 타이프에 의해 사용되는 import * as something from some

답변

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

를 사용하여 잘못하고 있어요, 그들은 transpiled되지 않습니다, 당신은 그 생성 된 JS에 표시되지 않습니다. IDE가 프로젝트의 모든 ts 파일을 인식하면 참조 주석이 필요하지 않습니다. 수입/수출이 transpiled 때문에, 그들은 JS 컴파일됩니다

당신은 당신이 현재 파일에 사용/내보내기 네임 스페이스/모듈을 가져올 수있다, 즉, 당신은 생성 된 JS

에서 그들을 볼 수 있습니다