So that's classes properties and inheritance with typescript.
And as I mentioned to these features also generally are supported in JavaScript in modern javascript
of course with the exception of type assignments and also with the exception of protected private public
these keywords are really only available in typescript.
Now it's not all what you can do with classes though when ever need feature which you also have in vanilla
javascript and which is also supported in typescript R getters and setters.
Now let's see that here on the accounting department for example let's say there we have a lost report
property which is a private property and it's of type String.
So we have that.
And then here we initialize this to be our reports property or this argument we're getting here and
there.
The first value which of course is basically undefined if we pass in an empty reports array.
Now when we add a report then don't want to set this last report equal to text which is what we add
to our report's array.
Now last report is private so we can access it from inside this method.
But we won't be able to access it from outside with the dot notation.
Now we can add a getter to still make it accessible a getter is basically a property where you execute
a function or a method when you retrieve a value.
And that allows you as a developer to add more complex logic here.
You create a getter by using to get keyword and then any name of your choice.
Typically closely related to the property you're trying to control the access to.
For example here we could name it most recent report.
Anything like that.
Now import does is defined like a method.
So you add parentheses and then curly braces.
And now in here you have to return something that's important getter method has to return something.
So here we could return this last report and then we kind of encapsulate this because this is publicly
accessible now.
But often you want to have more complex logic here.
So here we could check if we have a value here.
So if this is true.
If it's not undefined then we want to return last report.
Otherwise you might want to return some different logic.
For example let's say otherwise we throw an error message with throw in you.
Error no report found.
So now we added more logic more complex logic to this property and when we want to retrieve it we can
directly retrieve his property through the dot notation but we can use this as a property to Deandra
on this logic.
So how what does work well down there where I create my accounting department before I add my report
if I tried to console lock accounting dot most recent report and now important accounting dot most recent
report and now important You access this as a property so no parentheses here you don't execute does
as a method you just access it like a normal property.
And behind the scenes this will execute this method up there.
So if I tried this now before we added a report we'll see that this rebuilt without errors recompile
without errors.
But of course here an error is thrown.
It's our custom no report found error because no report was added yet.
And since using this as a getter accesses this method here and executes it if we have no report we don't
make it into this check we're into this if statement in day out for this line here it kicks in as an
alternative if we would move that down after we added a report then of course we can save that and we
no longer get an error but instead we output this last report.
So that's a getter and you can also add a setter a setter is added almost the same way you used the
set keyword.
And then again any name of your choice typically a name related to the property which should be set.
So here I will reuse most recent report so that we can use this both to read a value and to set a value.
And then again you defined us like a method.
But now this needs to take an argument the value which the user would have passed in here.
This should be a string value.
And now in here you can run any logic you want to store this.
Now here since we're passing in a value for the most recent report I in the end want to execute add
report.
So here a setter could simply be an alternative to the ADD report method.
So in here we can access this add report this is required since we're inside of the class.
We refer to the class and then to this class method here and now we can just forward value here.
Now we could add a more complex logic and also check if value is maybe not the find if it's false see
and then just return or throw a new error where we say Please pass in a valid value that is something
we could also do.
And now we're using a setter here as well.
With that added We can test it down there.
I could add most recent report I could access does on accounting and just like this we would read from
it.
But now by adding a equals sign we're trying to set a value to that and that will trigger this setter
method again just as before we don't execute it as a method but simply access is like a property now
here if I pass in an empty string then we technically do pass in a string but an empty string will be
treated as false see here and therefore we should now get an error.
So if I save this line of code if I save the code with this line of code added we indeed get this error
please pass it in a valid value.
If I do pass an invalid value though if I say year end report here for example and I now save this.
Now you will see this all works and year end report as part of our reports list.
So these are getters and setters which can be great for encapsulating logic and for adding extra logic
that should run when you try to read a property or when you try to set a property.