I want to clear my concept in implementation of an interface on a class.
An interface is something like a template which cannot make an impact until a class implements it. (link)
So if I define an interface as:
interface IVehicle {
color: string,
model_no: number,
}
And then I make a class as:
class Vehicle implements IVehicle {
}
It's giving me red underline at class name. Why I must declare the fields again in the class as it is implementing an interface it must not fetch its fields?
Why we must write like this?
class Vehicle implements IVehicle {
color: string;
model_no: number;
}
Then what is the concept of interfaces that a class can't fetch the fields of an interface whose it implements, what if I don't implement an interface and directly declare the fields in a class. I am thinking that why developers of TypeScript added this thing? It doubles up the code; first make an interface, declare fields in there, than make a class (also add implements InterfaceName
), then again declare those fields in that class, why?