The purpose of this project is to demonstrate how to use generics when programming in TypeScript.
The use case for this project is document processing class. The document being processed is an implementation
of either the IWebDocument
or IPrintDocument
interface. There are three example classes that use generic variables in
various ways. The document processing classes are DocumentProcessorV1
, DocumentProcessorV2
, and DocumentProcessorV3
DocumentProcessorV1
is an example of using a single generic variableT
that represents the document to process. The generic variableT
is constrained to accept only an implementation of anIWebDocument
or anIPrintDocument
interface.DocumentProcessorV2
is an example of using a generic variable at the class's method level only.DocumentProcessorV3
is an example of using two generic variables,T
andV
. Generic variableT
represents the document to be processes. The generic variableV
represents a confirmation of typeIWebConfirmation
orIPrintConfirmation
. The confirmation is returned by the class'sprocess(document: T) : V
method. The generic variableT
is constrained to accept only an implementation of anIWebDocument
or anIPrintDocument
interface.
This project has the following requirements
- Node.js and npm need to be installed on the machine running the project.
Execute the following instruction to install the project's dependencies from NPM.
npm install
You can view the code that the following instruction exercises here. The class being
exercised is DocumentProcessorV1
.
npx ts-node ./src/example-01.ts
You'll get output similar to the following:
Processing as a web document: https://incompatible-nickname.org/
Processing as a print document with page count: 39
You can view the code that the following instruction exercises here.
The class being exercised is DocumentProcessorV2
.
npx ts-node ./src/example-02.ts
You'll output similar to the following:
Processing as a web document: https://tender-sweatshop.name/
Processing as a print document with page count: 95
The following instruction that you can view here exercises a class that
takes two generic variables. One variable represents the document that the DocumentProcessor
class is to process. The other generic variable represents the confirmation that is returned by the
class method that processes the document.
The class being exercised is DocumentProcessorV3
.
npx ts-node ./src/example-03.ts
You'll get output similar to the following:
Processing as a web document: https://eager-loop.name/
{
webResult: {
timeStamp: 2024-03-31T12:56:21.520Z,
documentId: '6e0a6c5c-3821-4eb3-88d7-60019e639883',
title: 'causa amitto arceo tenetur',
url: 'https://eager-loop.name/'
}
}
Processing as a print document with page count: 28
{
printResult: {
timeStamp: 2024-03-31T12:56:21.525Z,
documentId: '64d0d367-c289-485f-bc46-b05b9810e460',
title: 'caterva absum adnuo quos',
pageCount: 28
}
}