Interfaces | Typescript
Readonly properties#
Some properties should only be modifiable when an object is first created.
You can construct a Point by assigning an object literal. After the assignment, x and y can’t be changed.
TypeScript comes with a ReadonlyArray<T> type that is the same as Array<T> with all mutating methods removed
Excess Property Checks#
Function Types#
Indexable Types#
There are two types of supported index signatures: string and number.
You can make index signatures readonly in order to prevent assignment to their indices:
Class Types#
Interfaces describe the public side of the class, rather than both the public and private side.
When a class implements an interface, only the instance side of the class is checked. You would need to work with the static side of the class directly.
Extending Interfaces#
Hybrid Types#
One such example is an object that acts as both a function and an object, with additional properties:
Interfaces Extending Classes#
Interfaces inherit even the private and protected members of a base class. This means that when you create an interface that extends a class with private or protected members, that interface type can only be implemented by that class or a subclass of it.
This is useful when you have a large inheritance hierarchy, but want to specify that your code works with only subclasses that have certain properties. The subclasses don’t have to be related besides inheriting from the base class. For example:
In the above example, SelectableControl contains all of the members of Control,
including the private state property.
Since state is a private member it is only possible for descendants of Control to implement SelectableControl.
This is because only descendants of Control will have a state private member that originates in the same declaration, which is a requirement for private members to be compatible.