February 2019
Beginner
694 pages
18h 4m
English
Interface definitions may also include optional properties, similar to the way that functions may have optional properties. Consider the following interface definition:
interface IOptionalProp {
id: number;
name?: string;
}
Here, we have defined an interface named IOptionalProp, which has an id property of type number, and an optional property called name that is of type string. Note how the syntax for optional properties is similar to what we have seen for optional parameters in function definitions. In other words, the ? character after the name property is used to specify that this property is optional. We can therefore use this interface definition, as follows:
let idOnly : IOptionalProp = { id: 1 }; let idAndName ...Read now
Unlock full access