This project demonstrates the use of decorators in TypeScript to modify and enhance class behavior.
- Class Decorator:
SimpleLoggerlogs the instantiation of a class. - Method Decorator:
LogMethodlogs method calls and their arguments. - Accessor Decorator:
MyReadOnlymakes a property read-only.
- Clone the repository:
git clone <https://github.com/el-riber/mob3>
cd typescript-decorators- Install dependencies:
npm install- Build the project:
npm run build- Run the project:
npm start- src: Contains the source TypeScript files.
MyTestClass.ts: Contains theMyTestClassclass with applied decorators.decorators.ts: Contains theSimpleLogger,LogMethod, andMyReadOnlydecorators.index.ts: Entry point for testing the decorators.
- dist: Contains the compiled JavaScript files.
- tsconfig.json: TypeScript configuration file.
- package.json: Project configuration and dependencies.
Logs when an instance of MyTestClass is created:
@SimpleLogger
class MyTestClass {
// class implementation
}Logs method calls and arguments:
@LogMethod
method1(): void {
console.log(`Method1 called: prop1 = ${this.prop1}`);
}Prevents modification of a property:
@MyReadOnly
get prop3(): string {
return this._prop3;
}In the src/index.ts file, we test the decorators by creating instances and calling methods/accessors.
import MyTestClass from './MyTestClass';
const instance = new MyTestClass('test', 42);
instance.method1();
instance.method2();
console.log(`Property prop3: ${instance.prop3}`);
try {
(instance as any).prop3 = 'new value'; // This should throw an error
} catch (e) {
if (e instanceof Error) {
console.error('Error:', e.message); // This should log the error message
} else {
console.error('An unknown error occurred');
}
}
const anotherInstance = new MyTestClass('anotherTest', 99);
anotherInstance.method1();
anotherInstance.method2();
console.log(`Property prop3: ${anotherInstance.prop3}`);You should see logs similar to:
SimpleLogger: Creating instance of MyTestClass
Calling method1:
LogMethod: Calling method1 with []
Method1 called: prop1 = test
Calling method2:
LogMethod: Calling method2 with []
Method2 called: prop2 = 42
Accessing prop3:
default
Trying to modify prop3:
Error: Cannot set property prop3
Creating a new instance of MyTestClass:
SimpleLogger: Creating instance of MyTestClass
Calling method1 on anotherInstance:
LogMethod: Calling method1 with []
Method1 called: prop1 = anotherTest
Calling method2 on anotherInstance:
LogMethod: Calling method2 with []
Method2 called: prop2 = 99
Accessing prop3 on anotherInstance:
default
This output confirms that the decorators are working as expected, logging method calls, preventing property modification, and logging class instantiation.
This project is licensed under the ISC License.