typescript override method code example
Example 1: ts override method
class Person{
name:string
eat():void{
console.log(this.name+" eats when hungry.")
}
}
class Student extends Person{
rollnumber:number;
constructor(rollnumber:number, name1:string){
super();
this.rollnumber = rollnumber
this.name = name1
}
displayInformation():void{
console.log("Name : "+this.name+", Roll Number : "+this.rollnumber)
}
eat():void{
console.log(this.name+" eats during break.")
}
}
var student1 = new Student(2, "Rohit")
student1.displayInformation()
student1.eat()
Example 2: typescript override type of property
interface A {
x: string
}
export type B = Omit<A, 'x'> & { x: number };
Example 3: typescript extension getter
interface A {
testGet: SomeType|null;
}
Object.defineProperty(A.prototype, "testGet", {
get (this: A) {
if(this.something) {
return this.something;
}
return null;
},
enumerable: false,
configurable: true
});