TypeScript Properties
Skills:
Systems Design Basics60%
Key Takeaways
The video covers TypeScript properties, including getters and setters, and how they differ from fields. It also discusses the use of properties for controlling access to private fields and performing validation.
Full Transcript
all right now this implementation has a tiny problem we can set the initial coordinate of this point and we can also draw it but there is no way for us to read the coordinate so I cannot access point that the X here so display to the user what's the workaround well one simple solution is to define a method like this yet X and here we simply return this that eggs because in this class we do have access to all the private members of this class but we cannot access them from the outside ok now here I can always call point that get X to get the x value and display to the user now let's talk about another use case maybe we want to give the user the ability to set the initial coordinate here but we also want them to be able to change this coordinate later only if they provide a value within a given range what do you mean by that let me show you I'm gonna define another method here set X now this method is gonna get a value that's the new value for the eggs field let me scroll down now here first we can do some basic validation so if value is less than zero you want to throw an error throw new error value cannot be less than zero otherwise we want to set this well x2 this new value okay now with this implementation we can always change the value of the X field like this point that set X we set it to a new value now if you have a use case like that in your applications you can use what we call a property so in typescript and in a lot of object-oriented programming languages we have a concept called property which is exactly for this very use case so look at how I can define a property here we start with a keyword which is get or set and then the name of the property which is in this case X and after that we're gonna have parentheses just like a method okay now similarly I'm gonna change this to set with a space so we have the set keyword and here it's like we have a function and method now what is the difference the difference is that we can use these properties like fields so here I can read X like this dot note the icon of X is the same icon we have for fields it's not a metal anymore so we can read X and we can also set it like this point that eggs are set it to 10 we don't have to call a method like this it's a cleaner syntax okay so this is what properties are for if you have private fields that you want to give maybe a read-only access to the outside or if you want to give the consumer of your classes the ability to set the values but you want to have some basic validation that's when you use a property now in this case if I want to give only the read-only access to this underlying field I can simply comment out the setter so we call this method a setter and the other method a getter okay I now look at this compilation error we cannot change the value of x now let's bring this back one last thing before we finish this lecture so here I have used a capital X for the name of my X property in JavaScript and in types crib we use camel notation to name our fields so that's why earlier we define this private field here using camel casing notation camel casing means the first letter of the first word is lowercase and the first letter of every word after is uppercase now what should we do to use camel casing notation for our properties if I name this the lowercase eggs it clashes with the existing field so let me revert this back a convention we use to solve this problem is to prefix the name of the underlying field with an underline so let's rename this using f2 and prefix it with an underline okay similarly for the Y parameter or the Y field I'm also going to use underline Y then we can rename this property from capital X to lowercase X once again we press f2 lowercase X and note that both instances was the getter and the setter are updated now we can work with this X property exactly the same way we use the X field so here's the lesson a property looks like your field from the outside but internally it's really a method in the class well more accurately it's either one method which is a getter or a setter or a combination of a getter and a setter hi thank you for watching my angular tutorial if you enjoyed this video please like it and share it with others also you can subscribe to my channel for free new videos every week this video is part of my complete angular course with almost 30 hours of high quality content where you learn everything about angular from the basic to the advanced topics all in one course so you don't have to jump from one tutorial to another in case you're interested you can get this course with a big discount using the link in the video description and if not that's perfectly fine continue watching as the next section is coming up
Original Description
🔥Get the COMPLETE COURSE (60% OFF - LIMITED TIME): https://programmingwithmosh.com/courses/angular
Learn about TypeScript properties and how they are different from fields.
RELATED VIDEOS
TypeScript for Angular / React Developers:
https://youtu.be/NjN00cM18Z4
TypeScript Classes:
https://youtu.be/W9Ah_ZDFc1c
TypeScript Constructors:
https://youtu.be/tiWqCBXnWj0
TypeScript Interfaces:
https://youtu.be/P17bFRuefjA
TypeScript Access Modifiers:
https://youtu.be/QygRnETpBIg
I have several other courses on web and mobile application development. You can find them all here:
http://programmingwithmosh.com/courses
STAY IN TOUCH
https://www.facebook.com/programmingwithmosh/
https://twitter.com/moshhamedani
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Programming with Mosh · Programming with Mosh · 45 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
▶
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
6 Visual Studio Tips to Increase Your Productivity | Mosh
Programming with Mosh
Visual Studio Keyboard Shortcuts that Speed Up Debugging Applications | Mosh
Programming with Mosh
Backbone.js Tutorial Part 2 - Backbone.js Models: Working with Model Attributes
Programming with Mosh
Backbone.js Tutorial Part 3 - Backbone.js Models: Model Validation
Programming with Mosh
Backbone.js Tutorial Part 4 - Backbone.js Models: Model Inheritance
Programming with Mosh
Backbone.js Tutorial Part 1 - Backbone.js Models: Creating Models
Programming with Mosh
Backbone.js Tutorial Part 5 - Backbone.js Models: Syncing Models with the Server
Programming with Mosh
Backbone.js Tutorial Part 6 - Backbone.js Collections: Creating Collections
Programming with Mosh
Backbone.js Tutorial Part 7 - Backbone.js Collections: Working with Collections
Programming with Mosh
Backbone.js Tutorial Part 8 - Backbone.js Collections: Fetching Collections from the Server
Programming with Mosh
Backbone.js Tutorial Part 9 - Backbone.js Views: Creating Views
Programming with Mosh
Backbone.js Tutorial Part 10 - Backbone.js Views: Passing Data to Views
Programming with Mosh
Backbone.js Tutorial Part 11 - Backbone.js Views: Handling the DOM Events
Programming with Mosh
Backbone.js Tutorial Part 12 - Backbone.js Views: Handling the Model Events
Programming with Mosh
Backbone.js Tutorial Part 13 - Backbone.js Views: Handling Collection Events
Programming with Mosh
Backbone.js Tutorial Part 14 - Backbone.js Views: Templating
Programming with Mosh
Clean Code: Learn to write clean, maintainable and robust code
Programming with Mosh
C# Events and Delegates Made Simple | Mosh
Programming with Mosh
C# Generics Tutorial: Whats and Whys | Mosh
Programming with Mosh
Debugging C# Code in Visual Studio | Mosh
Programming with Mosh
Repository Pattern with C# and Entity Framework, Done Right | Mosh
Programming with Mosh
Angular 2 Tutorial for Beginners: Learn Angular 2 from Scratch | Mosh
Programming with Mosh
Architecture of Angular 2+ Apps
Programming with Mosh
Working with Components in Angular
Programming with Mosh
C# Tutorial For Beginners - Learn C# Basics in 1 Hour
Programming with Mosh
Difference between Junior and Senior Developers
Programming with Mosh
Step-by-step ASP.NET MVC Tutorial for Beginners | Mosh
Programming with Mosh
[Pluralsight]: Become a Full-stack .NET Developer
Programming with Mosh
Xamarin Forms Tutorial: Build Native Mobile Apps with C#
Programming with Mosh
Value Types and Reference Types in JavaScript
Programming with Mosh
Using Redux in Angular 2+ Apps | Mosh
Programming with Mosh
Testing Angular 2+ Apps with Jasmine and Karma | Mosh
Programming with Mosh
Profile and optimize your Angular 2 apps
Programming with Mosh
Build a Real-world App with ASP.NET Core and Angular 2
Programming with Mosh
Entity Framework 6 Tutorial: Learn Entity Framework 6 from Scratch
Programming with Mosh
Two-way Binding and ngModel in Angular 4
Programming with Mosh
Udemy Live 2017: Teaching Tech Panel
Programming with Mosh
Demo of An E-commerce App Built with Angular, Firebase and Bootstrap 4
Programming with Mosh
My Brand New Angular Course
Programming with Mosh
TypeScript Tutorial - TypeScript for React - Learn TypeScript
Programming with Mosh
Access Modifiers in TypeScript
Programming with Mosh
TypeScript Interfaces
Programming with Mosh
TypeScript Classes
Programming with Mosh
TypeScript Constructors
Programming with Mosh
TypeScript Properties
Programming with Mosh
Angular Tutorial for Beginners: Learn Angular & TypeScript
Programming with Mosh
AngularJS vs Angular 2 vs Angular 4 | Mosh
Programming with Mosh
Angular Material Tutorial | Mosh
Programming with Mosh
Angular Animations Tutorial | Mosh
Programming with Mosh
Firebase in Angular Applications | Mosh
Programming with Mosh
Deploying Angular Applications | Mosh
Programming with Mosh
Building Forms in Angular Apps | Mosh
Programming with Mosh
Directives in Angular Applications
Programming with Mosh
Routing and Navigation in Angular | Mosh
Programming with Mosh
Angular 4 in 40 Minutes
Programming with Mosh
[NEW COURSE] Unit Testing for C# Developers
Programming with Mosh
Unit Testing C# Code - Tutorial for Beginners
Programming with Mosh
C# Classes Tutorial | Mosh
Programming with Mosh
C# Object Initializers Tutorial
Programming with Mosh
C# Constructors Tutorial | Mosh
Programming with Mosh
More on: Systems Design Basics
View skill →Related AI Lessons
⚡
⚡
⚡
⚡
Next.js vs Remix vs SvelteKit: Which Framework Should You Learn?
Dev.to · Etrit Neziri
Had my Frontend Developer interview with Capgemini (Application Developer) today, and I wanted to…
Medium · JavaScript
10 Frontend Developer Tools to Boost Productivity in 2026
Medium · Programming
10 Frontend Developer Tools to Boost Productivity in 2026
Medium · JavaScript
🎓
Tutor Explanation
DeepCamp AI