Lab - Use interfaces in TypeScript
In this lab, you'll convert some JavaScript code to strongly typed code using interfaces.
The JavaScript code contains two functions: calculateInterestOnlyLoanPayment
, which calculates the payment for an interest only loan, and calculateConventionalLoanPayment
, which calculates the payment for a conventional loan. As with most loan calculations, both functions accept principal
and interestRate
parameters. The difference between them is that the calculateConventionalLoanPayment
function accepts a third property, months
that the calculateInterestOnlyLoanPayment
function does not.
Property | Description |
---|---|
principal |
The principal amount of the loan. |
interestRate |
The annual interest rate of the loan. For example, 5% is specified as 5. |
months |
The term of the loan specified in months. An interest only loan does not require this property because the number of months is irrelevant (the loan will never be repaid when an interest only payment is made each month.) |
In this exercise, you will:
- Declare an interface called
Loan
that defines two properties,principal
andinterestRate
. - Declare an interface called
ConventionalLoan
that extendsLoan
, and defines the additional property required for a conventional loan,months
. - Update the two functions to implement the new interfaces and strongly type the parameters.
Exercise 1 - Declare the interfaces
Clone the starting repository by entering the following at the command prompt.
git clone https://github.com/MicrosoftDocs/mslearn-typescript cd mslearn-typescript/code/module-03/m03-start code .
Open the file module03.ts.
Locate
TODO: Declare the Loan interface
. Declare an interface calledLoan
that defines two properties,principal
andinterestRate
, each as anumber
.interface Loan { principal: number, interestRate: number //* Interest rate percentage (eg. 14 is 14%) }
Locate
TODO: Declare the ConventionalLoan interface
. Declare an interface calledConventionalLoan
that extendsLoan
, and defines the additional property required for a conventional loan,months
, as anumber
.interface ConventionalLoan extends Loan { months: number //* Total number of months }
Exercise 2 - Implement the interfaces
Locate
TODO: Update the calculateInterestOnlyLoanPayment function
. Replace the two parameters in thecalculateInterestOnlyLoanPayment
function with an object of typeLoan
(for example,loanTerms: Loan
), and enter the return value of the function as astring
.function calculateInterestOnlyLoanPayment(loanTerms: Loan): string { // ... }
You'll notice a couple of errors because TypeScript does not recognize the parameters
interestRate
andprincipal
. Replace the parameter names in the function with properties of theLoan
object. (For example,loanTerms.interestRate
).function calculateInterestOnlyLoanPayment(loanTerms: Loan): string { // Calculates the monthly payment of an interest only loan let interest = loanTerms.interestRate / 1200; // Calculates the Monthly Interest Rate of the loan let payment; payment = loanTerms.principal * interest; return 'The interest only loan payment is ' + payment.toFixed(2); }
Enter the
interest
andpayment
variables in thecalculateInterestOnlyLoanPayment
function asnumbers
.Test the
calculateInterestOnlyLoanPayment
function to verify that it is working correctly. Remember that you must now pass the parameters to the function in the form of aLoan
object.let interestOnlyPayment = calculateInterestOnlyLoanPayment({principal: 30000, interestRate: 5}); console.log(interestOnlyPayment); //* Returns "The interest only loan payment is 125.00"
Locate
TODO: Update the calculateConventionalLoanPayment function
. Update thecalculateConventionalLoanPayment
function, this time replacing the three parameters with an object of typeConventionalLoan
, and enter the return value of the function as astring
. Make any remaining updates to the implementation of thecalculateConventionalLoanPayment
function.function calculateConventionalLoanPayment(loanTerms: ConventionalLoan): string { // Calculates the monthly payment of a conventional loan let interest: number = loanTerms.interestRate / 1200; // Calculates the Monthly Interest Rate of the loan let payment: number; payment = loanTerms.principal * interest / (1 - (Math.pow(1/(1 + interest), loanTerms.months))); return 'The conventional loan payment is ' + payment.toFixed(2); }
Test the
calculateConventionalLoanPayment
function to verify that it is working correctly. Remember that you must now pass the parameters to the function in the form of aConventionalLoan
object.let conventionalPayment = calculateConventionalLoanPayment({principal: 30000, interestRate: 5, months: 180}); console.log(conventionalPayment); //* Returns "The conventional loan payment is 237.24"
Lab solution
View the final version of the code by entering the following at the command prompt.
cd ../m03-end
code .
Open the file module03.ts to see the solution to this lab. See the Lab setup section above for more information about setting up your development environment to run the solution.