2016-09-03 4 views
1

私はTSモジュール内でエクスポートする必要があるモデルとサービスをたくさん持っています。 最終的なTypsecriptエクスポートをモジュールでラップしますか?

モデル/ User.ts

import {IModel} from "./IModel"; 

export interface User extends IModel { 
    // ... 
} 

サービス/ UserService.ts

import {inject} from 'aurelia-framework'; 
import {HttpClient} from 'aurelia-fetch-client'; 
import {BaseService} from "./BaseService"; 
import {User as ModelType} from "../models/User"; 

@inject(HttpClient) 
export class UserService extends BaseService<ModelType> { 
    // ... 
} 

ディスト

ソースはここで、私は最終的に何をしたいのですビルド後プロジェクトを掘り起こす。必ずしもそのままではありませんが、一般的に私のルートモジュールmy-moduleにはmodelsのサブモジュールとservicesのサブモジュールが含まれている必要があります。あるいは、モデル/サービスが同じ「コンテナ」の下にバンドルされていないように、 (例えば、モデル/サービス間の名前の競合を避けるため)。

DIST/index.d.ts

declare module "my-module/models" { 
    export interface User { ... } 
    // ... other models here ... 
} 

declare module "my-module/services" { 
    export class UserService { ... } 
    // ... other models here ... 
} 

私はnpmを経由して、このパッケージをインストールして、このような別のプロジェクトでそれを使用できるように:

import {User} from "my-module/models"; 
import {UserService} from "my-module/services"; 
  • ありますモデル/サービスがたくさんあるので、私は手作業で維持/構築して、index.tsをビルドする必要はありません。
  • Typescriptプロジェクトをどのように構造化すればよいですか?

答えて

1

typings bundleはまさにあなたが必要としているようです。すべての宣言ファイルと依存関係を1つのファイルにまとめます。

たとえば、次のようにします。私は2つのファイル(stackoverflow.tsとconstants.ts)を持っている "stackoverflow"と呼ばれるモジュールを持っています。 index.tsは外部ライブラリ(例:bluebird)とconstants.tsの両方に依存します。

stackoverflow.ts

import { PromisifyOptions } from 'bluebird' 
export { FIRST } from './constants' 

export interface thePromiseUsingInterface { 
    options: PromisifyOptions 
} 

constants.ts

export const FIRST = 1 
export const SECOND = 2 

マスター宣言ファイルを生成するには、次

  1. あなたの宣言ファイルを生成:tsc --delcarationを)
  2. index.d.tsに宣言ファイルとその依存関係をバンドル: typings bundle -o index.d.ts

また、あなたのtypings.jsonが含まれていることを確認します、NPMにあなたのライブラリーを公開する場合。他のプロジェクトで

は、あなたがしてnpm install stackoverflowし、それを使用して起動することができます:ここに参考のため

import * as constants from 'stackoverflow/constants' 
console.log(constants.SECOND) 

gist with all the relevant filesです:stackoverflow.ts、constants.ts、index.d.ts、package.json、タイピング

関連する問題