TypeScript Tutorial in Hindi #39 - Override Function with Decorator in TypeScript

Override function with Decorator in Typescript


decorator-with-function.ts

function updatedSum(target:any,key:string,descriptor:PropertyDescriptor){
descriptor.value=function sum(x:number,y:number){
let output=x+y
return "the output of "+x+" and "+y+ " is : " + output
}
}

class CustomMaths1{

@updatedSum
sum(x:number,y:number){
return x+y
}
}
var cm2= new CustomMaths1();
console.log(cm2.sum(10,20));


decorator-with-function.js

"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
function updatedSum(target, key, descriptor) {
descriptor.value = function sum(x, y) {
let output = x + y;
return "the output of " + x + " and " + y + " is : " + output;
};
}
class CustomMaths1 {
sum(x, y) {
return x + y;
}
}
__decorate([
updatedSum
], CustomMaths1.prototype, "sum", null);
var cm2 = new CustomMaths1();
console.log(cm2.sum(10, 20));