3

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?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • If you only have one implementation, and it implements only one interface, it may seem pointless to define both an interface *and* a class. But imagine you had multiple implementations of your interface, or classes that implemented multiple interfaces (read up on ISP, the I in SOLID). Also you can meet interfaces in different ways (e.g. a plain property vs. a getter and setter) in different implementations. – jonrsharpe Apr 27 '19 at 08:00
  • Well, you can work against the interfaces. An interface just describes what is there to any consumer, the implementation can hold lots other things. For any consumer, they can work with `IVehicle`, as this is the defined contract, and they don't have to care what is the implementation like or what other kind of fields are still there. The interface simply defines a contract that you can work against – Icepickle Apr 27 '19 at 08:01
  • As you mentioned interface is here just for contracts. Maybe abstract class is here to help you. – Pavel Kratochvil Apr 27 '19 at 08:12
  • My question is still there. I am asking why? –  Apr 27 '19 at 08:29
  • 1
    Because you can then work against defined contracts, and not against concrete implementations. For your concrete implementation to work, it must match the defined contract. – Icepickle Apr 27 '19 at 13:51

1 Answers1

2

because this is also valid:

interface IVehicle {
    color: string;
    model_no: number;
}

class ClownCar implements IVehicle {
    // can be a subset of original type
    public color: 'red' | 'green' | 'blue';
    public model_no: 250 = 250;
}

class NonEditableCar implements IVehicle {
    // can use getter or setters instead, doesn't have to be normal field
    get color() {
        return 'grey';
    }
    get model_no(){
        return 100;
    }
}

An interface just says that an instance will have those fields, it doesn't say it must match that exact type. The declaration you have in the class specifies that the implementation is to store an instance variable which needs to be initialized.

You can narrow instance variables and widen method call signatures while still implementing an interface:

interface VehicleCarrier {
    contents: IVehicle[];
    launch(count: number): void;
}

class AircraftCarrier implements VehicleCarrier {
    // this is more specific, and because the inteface forces you to write it you have to consider that
    // it should be more specific.
    public contents: (Plane | Submarine)[];
    // can extend call signatures for methods
    public launch(count: number, type: 'plane' | 'sub' = 'plane') {}
}
Tadhg McDonald-Jensen
  • 20,699
  • 5
  • 35
  • 59
  • 2
    Also, more importantly: the interface just provides the type signature, the class provides the actual implementation (the body of a method, the value of a data property). – Bergi Apr 28 '19 at 13:22
  • @Bergi I interpreted the question as that needing to implement it was obvious, just why it needed to be re-declared. My goal was to point out that the declaration is part of the implementation. – Tadhg McDonald-Jensen May 12 '19 at 20:35