How to add Access Modifiers with Constructor Parameters in TypeScript

Yes in Typescript we can do that I mean we can add access modifiers in Constructor Function along with the parameters to control the access throughout out the class.

If you see in the last tutorial I have given details all you needs know about access modifier from typescript, if you have not checked yes please do it is essential here is the link

What is Access Modifiers and how to use Access Modifiers in TypeScript ?

As you start growing TypeScript Developer or essentially a Javascript Developer you should start making habit of using best practices it will help you to be expert developer.

Constructor Function without Access Modifiers

Typescript has a very good feature of removing redundant code of lines and refactoring it, so if you see the following typescript class we added two private variables and similarly we have two parameters for the constructor to assign variables.

Typescript Refactoring Access Modifiers

Refactoring TypeScript class by adding Access Modifiers

Now let’s see how to refactor the above class by adding Access Modifiers in constructor parameters.

You can go ahead and remove variable declaration from the line number 2 and 3 and prefix the constructor parameters with access modifiers and also remove initialization from the body of constructor function, like showing below:

Refactoring TypeScript class by adding Access Modifiers

Now if you see the class it looks much clean and simple to read if you compile above class and using typescript compiler at end typescript compiler will generate assigning part and declaration of this fields out of the box.

I have both the class here for your reference.

Original Typescript class:

Compiled typescript class into javascript:


As a developer It is alway good to use best practices into your programming everyday it will help you grow fast and achieve your target on time.

IMPORTANT NOTE – Before you go and read another tutorial I want to clarify one important thing here about adding modifiers in Constructor Function, no matter what access modifier you want to apply to the variables you have to prefix those with the modifiers, even for default public modifier.

For example if you decide to add public modifier even the default access is public you should still needs mention that in the Constructor otherwise your code will not work.

So from now always keep in mind to utilize this feature from Typescript to save your time as well as make you typescript code more clean, easy to maintain and read.

Yogesh Koli

Software engineer & Blogger live in India, has 8+ years of experience working with the Front-end and Back-end Web Application Development.

Recent Posts

Complete guide of using Laravel 6 Eloquent Subquery Enhancements

Learn How to use laravel frameworks new improved feature called Eloquent Subquery and get example…

3 years ago

3 Useful examples of using Array Map function in PHP – Best Practices

Learn how to use php array map function with easy and essential tutorial to modify…

3 years ago

Working with PHP Array Filter Function – Best Practices

Learn how to use php array filter function with easy and essential tutorial to filter…

3 years ago

What is Access Modifiers and how to use Access Modifiers in TypeScript ?

What is Access Modifiers in typescript, how to use Access Modifiers, when to use them,…

3 years ago

Top 10 Super Useful Packages to Improve Laravel applications in 2019

This tutorial provide ultimate list of package those are top 10 on packagist and super…

3 years ago

How to secure pdf files In Laravel and allow access to authenticated users

This tutorial provides a perfect solution on how to protect and secure pdf file in…

3 years ago