interface IPlayer {
id: String;
name: String;
dob: String;
GetName<IPlayer>(): Promise<IPlayer>;
}
class Player implements IPlayer {
constructor(public id: String, public name: string, public dob:string) { }
GetName<IPlayer>(): Promise<IPlayer> {
let player: IPlayer = new Player("Hello", "World", '');
return new Promise<IPlayer>((resolve, reject) => {
resolve(player);
});
};
}
Not sure what I am doing wrong here. Can you please tell me why I am not able to create the instance of an interface in this code?
This is returning the error below:
Type 'Player' is not assignable to type 'IPlayer'.
let player: IPlayer ##
Can anyone help me with where I am doing this wrong in creating the instance of the interface?
The problem is you declare IPlayer
as a type parameter to GetName
. This generic type parameter has no relation to the interface IPlayer
. You can just remove the generic type parameter and it will work as expected:
interface IPlayer {
id: String;
name: String;
dob: String;
GetName(): Promise<IPlayer>;
}
class Player implements IPlayer {
constructor(public id: String, public name: string, public dob: string) { }
GetName(): Promise<IPlayer> {
let player: IPlayer = new Player("Hello", "World", '');
return new Promise<IPlayer>((resolve, reject) => {
resolve(player);
});
};
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With