If you already have a subscription, you can sign in.
Enjoy free content straight from your inbox 💌
00:00
Here we have a type representing a person with the member's name and email both of type string. Now these members of the type are considered required members. In addition to required members, we can add an optional member with a question mark modifier. Here we've added an additional optional phone member to the person type. Now we can create an instance of the person type with all three members, or we can even create a person object with just the two required members leaving out the optional phone member. Now in JavaScript, if you try to access a property
00:34
that hasn't been provided, the JavaScript runtime will return the special value. Undefined TypeScript understands that and we'll automatically mark all optional members to also include the undefined type. Now with optional members, we can leave them out as we have done for the Alfred object over here, or we can provide a member with the correct type. For example, provide a string for the phone as you previously did for the Bruce object as well. Or we can even provide an explicit undefined value. It is still type checked,
01:07
so anything other than the correct type. For example, providing a number when a string was annotated will result in a compile time error. Now, TypeScript also supports optional members on classes. Let's look at them with another example. Here we have a class with required members X and Y of type number. Now because we haven't provided any default value for these X and Y members of type number, nor have we initialized them in the class constructor, TypeScript is going to highlight this as a compiler error. Now, we can fix that by initializing these values in the
01:40
class constructor, or we can provide an INITIALIZER in line. Alternatively, now that we understand optional members, we can remove the initializer and simply mark these members as being optional. Now, because these members are optional, we can create an instance of the point class and not provide any initial values to the X and Y members. And if we try to read these members, the JavaScript runtime will return us the special value undefined. Once more, TypeScript understands that and automatically adds undefined to the number of possible values for optional members.
02:14
This means that we can assign it the correct type, which in our case is number, or we can even assign it to special value undefined. Now, one final thing worth mentioning for optional members PA types or classes is that null is not considered an optional value. So if we try to assign null to a particular member, it'll result in a compiled time error. If you want to support null as a possible value for any of the members of your types of classes, you will have to add that as an explicit annotation. For example, using a union type.