ラボ - 名前空間を使用してコードを編成する

完了

このラボでは、TypeScript を使用してコードを編成するためのさまざまな方法を試してみます。

演習 1

プロジェクトの TypeScript コードには、LoanConventionalLoan という 2 つのインターフェイスが含まれています。 また、3 つの関数も含まれています。

  • calculateInterestOnlyLoanPayment。インタレスト オンリー ローンの支払額を計算します。
  • calculateConventionalLoanPayment。従来型のローンの支払額を計算します。
  • calculateInterestRate。ローンの月利を計算するワーカー関数。

calculateInterestOnlyLoanPayment および calculateConventionalLoanPayment 関数では、principle および interestRate パラメーターを受け入れます。 これらの違いは、calculateConventionalLoanPayment 関数では 3 番目のプロパティである months を受け入れ、calculateInterestOnlyLoanPayment 関数では受け入れないことです。

プロパティ 説明
principle ローンの元金額。
interestRate ローンの年利。 たとえば、5% は 5 と指定します。
months 月単位で指定されたローンの期間。 インタレスト オンリー ローンでは、月数は関係ないため、このプロパティは必要ありません (毎月利息のみが支払われる場合、ローンが返済されることはありません)。

この演習では、単一の TypeScript ファイルで名前空間を使用してコードを編成します。

  1. コマンド プロンプトで次のように入力して、開始リポジトリを複製します。

    git clone https://github.com/MicrosoftDocs/mslearn-typescript
    cd mslearn-typescript/code/module-08/m08-start
    code .
    
  2. 開始ワークスペースで、module08_main ファイルを TypeScript エディターで開きます。

  3. TODO Create a the Loans namespace を特定します。

  4. Loans という名前の新しい名前空間を作成します。

  5. Loan インターフェイスと ConventionalLoan インターフェイスを Loans 名前空間に移動します。

  6. Loan インターフェイスと ConventionalLoan インターフェイスを更新して、Loans 名前空間の外側でそれらを使用できるようにします。

    namespace Loans {
       export interface Loan {
           principle: number,
           interestRate: number        //* Interest rate percentage (eg. 14 is 14%)
       }
       export interface ConventionalLoan extends Loan {
           numberOfMonths: number      //* Total number of months
       }
    }
    
  7. TODO Create LoanPrograms namespace を特定します。

  8. LoanPrograms という名前の新しい名前空間を作成します。

  9. 3 つの関数を LoanPrograms 名前空間に移動します。

    namespace LoanPrograms {
       /*  TODO Update the calculateInterestOnlyLoanPayment function. */
       function calculateInterestOnlyLoanPayment(loanTerms: Loan): string {
           let payment: number;
           payment = loanTerms.principle * calculateInterestRate(loanTerms.interestRate);
           return 'The interest only loan payment is ' + payment.toFixed(2);
       }
       /*  TODO Update the calculateConventionalLoanPayment function. */     
       function calculateConventionalLoanPayment(loanTerms: ConventionalLoan): string {
           let interest: number = calculateInterestRate(loanTerms.interestRate);
           let payment: number;
           payment = loanTerms.principle * interest / (1 - (Math.pow(1/(1 + interest), loanTerms.months)));
           return 'The conventional loan payment is ' + payment.toFixed(2);
       }
       function calculateInterestRate (interestRate: number): number {
           let interest: number = interestRate / 1200;
           return interest
       }
    }
    
  10. TODO Update the calculateInterestOnlyLoanPayment function を特定します。

  11. calculateInterestOnlyLoanPayment 関数で:

    1. Loans 名前空間が含まれるように、Loan インターフェイスへの参照を更新します。
    2. LoanPrograms 名前空間の外側で関数を使用できるようにします。
    export function calculateInterestOnlyLoanPayment(loanTerms: Loans.Loan): string {
        let payment: number;
        payment = loanTerms.principle * calculateInterestRate(loanTerms.interestRate);
        return 'The interest only loan payment is ' + payment.toFixed(2);
    }
    
  12. TODO Update the calculateConventionalLoanPayment function を特定します。

  13. calculateConventionalLoanPayment 関数で:

    1. Loans 名前空間が含まれるように、ConventionalLoan インターフェイスへの参照を更新します。
    2. LoanPrograms 名前空間の外側で関数を使用できるようにします。
     // Calculates the monthly payment of a conventional loan      
     export function calculateConventionalLoanPayment(loanTerms: Loans.ConventionalLoan): string {
         let interest: number = calculateInterestRate(loanTerms.interestRate);
         let payment: number;
         payment = loanTerms.principle * interest / (1 - (Math.pow(1/(1 + interest), loanTerms.months)));
         return 'The conventional loan payment is ' + payment.toFixed(2);
     }
    
  14. TODO Update the function calls を特定します。 これを開始コードと終了コードに追加してください。

  15. LoanPrograms 名前空間を関数の名前に追加します。

let interestOnlyPayment = LoanPrograms.calculateInterestOnlyLoanPayment({principle: 30000, interestRate: 5});
let conventionalLoanPayment = LoanPrograms.calculateConventionalLoanPayment({principle: 30000, interestRate: 5, months: 180});
  1. 作業を保存、コンパイル、およびテストします。

演習 2

この演習では、名前空間を複数の TypeScript ファイルに再編成します。

  1. 演習 1 からプロジェクトを続行します。

  2. ワークスペースに 2 つの新しい TypeScript ファイル (module08_loans.tsmodule08_loan-programs.ts) を作成します。

  3. Loans 名前空間を、module08_main.ts から module08_loans.ts に移動します。

  4. LoanPrograms 名前空間を、module08_main.ts から module08_loan-programs.ts に移動します。

  5. module08_loan-programs.ts の先頭に、module08_loans.tsmodule08_loan-programs.ts 内のインターフェイス間のリレーションシップを記述する reference ステートメントを追加します。

    /// <reference path="module08_loans.ts" />
    
  6. module08_main.ts に、TODO Add reference paths を配置します。

  7. module08_loans.tsmodule08_loan-programs.ts、および module08_main.ts 間のリレーションシップを記述する reference ステートメントを追加します。

    /// <reference path="module08_loans.ts" />
    /// <reference path="module08_loan-programs.ts" />
    
  8. コマンド プロンプトで、次のコマンドを実行して、すべての依存する .ts ファイルをコンパイルして main.js という名前の単一の JavaScript ファイルを作成します。

    tsc --outFile main.js module08_main.ts
    
  9. main.js ファイルを実行して、作業をテストします。

ラボのソリューション

コマンド プロンプトで次のように入力して、終了リポジトリを複製します。

git clone https://github.com/MicrosoftDocs/mslearn-typescript
cd mslearn-typescript/code/module-08/m08-end
code .

.ts ファイルを開いて、このラボのソリューションを確認します。 ソリューションを実行するための開発環境のセットアップについて詳しくは、前のラボの設定に関するセクションを参照してください。