As explained here the ?
operator can be used to mark a function parameter as optional. What does the ?
operator mean on interface parameters? For example if we have this typescript interface:
export interface Person {
phone?: number;
name?: string;
}
And a class that implements the interface:
class Customer implements Person {
}
Did Customer
now implement Person correctly because all the properties on the Person
interface are optional?
The short answer is yes, Customer
correctly implements Person
since all fields of the interface are optional any object will correctly implement the interface.
The usefulness of this interface is:
phone
has to be defined as number
) Person
(you should check if they are undefined
) but the function for example guarantees it will not access any other fields of a Person
parameter.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.