Let's come back to interfaces for objects.
You can also define optional properties in interfaces and also in classes as I will show you.
For Dad you add your property name let's say output name here can be whatever you want of course.
And that should be a string but I don't want to force every class based on name to have that.
It should be optional whether you want to have that or not.
You can specify an optional property by adding a question mark after the property name.
This tells typescript that this property might exist in classes that implement this interface but it
doesn't have to.
And therefore we get no error here in person either though I don't have the output Name property because
it is optional.
Now of course I can add output name here and initialize this with some value or use it like any other
property.
But I don't have to know on a class you can also have an optional property let's say name here should
be optional because maybe we don't get a value here for n we can add a question mark here as well in
our class we now adjust all to have to make it optional here in named otherwise we violate against our
interface which for assistance to be a string.
Now I'm saying this can be optional.
So here it's not optional and here when I set this equal to N I only want to do this if n is truthfully
if it is set.
So only if it's not an empty string for example otherwise name will not be set.
And Dad is okay because I turned out to be an optional property both here in the interface but also
in my class.
So now a year when we construct a new object we could do this without passing in a name to allow that
we have to go to the constructor and provide a default value here or also add a question mark here does
this also allowed.
You can add optional parameters either by providing a default full back value or by adding a question
mark in which case the default value will be undefined.
And now we can call this constructor without a value because we have that optional parameter and because
we have the optional property that is OK we don't need to initialize it and hence thereafter if we create
well this name of course will yield on the fine.
So probably we should check here as well if we got a name and if we do we can't lock this else we can
so lock high without any reference to the name because we don't have one if we save that everything
compiles and we see just high here because it didn't provide a name so that would be optional properties
both in a class and in an interface and also in my constructor list here.
And now just to make it really clear again these free things are only loosely related.
You can have an optional property in an interface and then implement it as a non optional property in
the class.
You then just have to make sure that your logic is such that this is always initialized otherwise you
get an error as you just saw or you have an optional property in an interface and an optional property
in your class then you don't have to assign a value in all cases here.
And in addition in all the totally unrelated here you can have optional parameters in functions and
therefore also in methods including the constructor method optional parameters are defined either with
the question mark where the default value if not set is therefore undefined or simply by assigning a
default value like this.
Then as you learned this is also optional and the default value will be assumed if you don't pass in
a more specific value.
So this gives you more flexibility in how you structure your classes.
And when we talk about interfaces how you structure your contracts classes might need to adhere to.