Currently, I am engaged in an online tutorial on typescript (although it is not in English, I will translate the example):
interface Message {
email: string;
receiver?: string;
subject?: string;
content: string;
}
The concept of the ?
in this context is to denote certain properties as optional. This raises a question that my tutorial does not explore - if interfaces in TypeScript are meant to serve as contracts ensuring specific properties are always present, what purpose do optional properties serve? After all, any additional or optional properties could simply be handled at the object or class definition level.
For instance, I can see the practicality of optional methods in Java interfaces due to their default method bodies that can be reused in implementing classes. However, the idea of optional properties in TypeScript seems somewhat perplexing upon initial examination.