{"id":1319,"date":"2023-08-15T05:00:00","date_gmt":"2023-08-15T12:00:00","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=1319"},"modified":"2023-08-14T15:28:16","modified_gmt":"2023-08-14T22:28:16","slug":"typescript-visibility-modifiers","status":"publish","type":"post","link":"https:\/\/www.angulartraining.com\/daily-newsletter\/typescript-visibility-modifiers\/","title":{"rendered":"Typescript visibility modifiers"},"content":{"rendered":"\n<p>Angular is mostly about Typescript classes, and Typescript classes have modifiers that alter the <a href=\"https:\/\/www.typescriptlang.org\/docs\/handbook\/2\/classes.html#member-visibility\" target=\"_blank\" rel=\"noopener\" title=\"\">visibility of class members<\/a>: <code>public<\/code>, <code>protected<\/code>, <code>private<\/code>, and <code>readonly<\/code>. Here is what you need to know about them.<\/p>\n\n\n\n<p><strong>Everything is public by default<\/strong><\/p>\n\n\n\n<p>If you don&#8217;t use a modifier on a class member, it is public by default. Any other class can see and use that member and change its value:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"390\" height=\"328\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-16.png\" alt=\"\" class=\"wp-image-1320\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-16.png 390w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-16-300x252.png 300w\" sizes=\"auto, (max-width: 390px) 100vw, 390px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p><strong><code>private<\/code> means that the member isn&#8217;t visible outside of the class (including a component&#8217;s HTML template)<\/strong><\/p>\n\n\n\n<p><code>private<\/code> is a way to enforce that other classes cannot access a member of your class. Used in a component, this indicates that we do not want that property to be used in the component&#8217;s HTML template. In a service, this suggests that we don&#8217;t want other components\/services to see that member:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"684\" height=\"122\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-17.png\" alt=\"\" class=\"wp-image-1321\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-17.png 684w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-17-300x54.png 300w\" sizes=\"auto, (max-width: 684px) 100vw, 684px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p><strong><code>protected<\/code> is in-between <code>public<\/code> and <code>private<\/code>. It makes the member accessible in a component&#8217;s template without making it fully public.<\/strong><\/p>\n\n\n\n<p>In the following example, our component property date is invisible from other Angular code in our app, but our HTML template can use it:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"502\" height=\"469\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-18.png\" alt=\"\" class=\"wp-image-1322\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-18.png 502w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-18-300x280.png 300w\" sizes=\"auto, (max-width: 502px) 100vw, 502px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p><strong>Best practice &#8211; My recommendation<\/strong><\/p>\n\n\n\n<p>If you want to stick to simple rules that make sense and are the safest, here&#8217;s what you can do:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Make every member <code>private<\/code> by default<\/li>\n\n\n\n<li>If the member is needed in the template of a component, make it <code>protected<\/code><\/li>\n\n\n\n<li>If the member is meant to be fully public, go with <code>public<\/code><\/li>\n<\/ol>\n\n\n\n<p>Tomorrow, I&#8217;ll add a couple more suggestions by introducing the <code>readonly<\/code> modifier. Stay tuned!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Angular is mostly about Typescript classes, and Typescript classes have modifiers that alter the visibility of class members: public, protected, private, and readonly. Here is what you need to know about them. Everything is public by default If you don&#8217;t use a modifier on a class member, it is public by default. Any other class [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,8,3],"tags":[],"class_list":["post-1319","post","type-post","status-publish","format-standard","hentry","category-angular","category-components","category-typescript"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1319","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/comments?post=1319"}],"version-history":[{"count":2,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1319\/revisions"}],"predecessor-version":[{"id":1328,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1319\/revisions\/1328"}],"wp:attachment":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=1319"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=1319"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=1319"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}