How to use class interface in TypeScript

2 Answers

0 votes
interface IPerson {
    iName: string;
    display():void;
    //getSalary(): void;
}

class Employee implements IPerson {
    eID: number;
    iName: string;
    
    constructor(ID: number, name: string) {
        this.eID = ID;
        this.iName = name;
    }
    
    display = () => console.log(this.eID + ' ' + this.iName)

    getSalary() : number {
        return 19250;
    }
}

let per:IPerson = new Employee(857421, 'Professor Albus Dumbledore');

per.display();

// console.log(per.getSalary()); // Property 'getSalary' does not exist on type 'IPerson'.(2339)
    
 

    
/*
    
run:
    
"857421 Professor Albus Dumbledore"
   
*/

 



answered Oct 25, 2021 by avibootz
0 votes
interface IPerson {
    iName: string;
    display():void;
    getSalary(): void;
}

class Employee implements IPerson {
    eID: number;
    iName: string;
    
    constructor(ID: number, name: string) {
        this.eID = ID;
        this.iName = name;
    }
    
    display = () => console.log(this.eID + ' ' + this.iName)

    getSalary() : number {
        return 19250;
    }
}

let per:IPerson = new Employee(857421, 'Professor Albus Dumbledore');

per.display();

console.log(per.getSalary()); 
    
 

    
/*
    
run:
    
"857421 Professor Albus Dumbledore"
19250 
   
*/

 



answered Oct 25, 2021 by avibootz

Related questions

1 answer 205 views
205 views asked Sep 11, 2020 by avibootz
1 answer 273 views
1 answer 294 views
1 answer 355 views
1 answer 292 views
1 answer 280 views
...