switchon@lavalamp.biz
+27(0) 83 419 4851 / +27(0) 21 036 1165
Application & software development
Get A Quote

    • Home
    • Services
      • Application & software development
      • Outsourced software development
      • Project based resourcing
      • Digital marketing & consulting
      • Graphic design & consulting
      • UI / UX design & consulting
      • Recruitment services
      • Lease an expert
    • About
      • How we work
      • NBConsult Group
      • Partners
      • Lightbox Digital
    • Blog
    • Join us
    • Contact

    • Home
    • Services
      • Application & software development
      • Outsourced software development
      • Project based resourcing
      • Digital marketing & consulting
      • Graphic design & consulting
      • UI / UX design & consulting
      • Recruitment services
      • Lease an expert
    • About
      • How we work
      • NBConsult Group
      • Partners
      • Lightbox Digital
    • Blog
    • Join us
    • Contact

    • Home
    • Services
      • Application & software development
      • Outsourced software development
      • Project based resourcing
      • Digital marketing & consulting
      • Graphic design & consulting
      • UI / UX design & consulting
      • Recruitment services
      • Lease an expert
    • About
      • How we work
      • NBConsult Group
      • Partners
      • Lightbox Digital
    • Blog
    • Join us
    • Contact

    • Home
    • Services
      • Application & software development
      • Outsourced software development
      • Project based resourcing
      • Digital marketing & consulting
      • Graphic design & consulting
      • UI / UX design & consulting
      • Recruitment services
      • Lease an expert
    • About
      • How we work
      • NBConsult Group
      • Partners
      • Lightbox Digital
    • Blog
    • Join us
    • Contact
    implementing laravel echo
    Implementing Laravel Echo And Socket.io With A Laravel API And Ionic Vue App
    Nov 9, 2021
    studies and courses to better yourself
    Studies And Online Courses To Better Yourself
    Nov 30, 2021

    Typescript Access Modifiers, Type Safety And Return Types And Why They Are Important

    typescript access modifiers
    typescript access modifiers

    Typescript offers ways for a developer to write cleaner and more robust code by making use of access modifiers, type safety and declaring return types.

    For the purposes of this article I will not be going through every property available for the above subject, but simply mentioning a few as examples.



    Access Modifers


    This is a way to declare a variable inside a class in order to restrict it's access or ability to be modified outside of the that class. Using the Public modifier, which Typescript takes as it's default access modifier if no modifier is specified, allows the variable to be read and modified outside of the class it was declared inside of.

    access modifier

    Another popular modifier is Private. This will ensure your variable is only read and modified inside of the class it was declared inside of.

    access modifier

    With only using these two modifiers, you can see that already your code will be more reliable. Access modifiers also have the added benefit of syntactic sugar. In other words, simply scanning over your code will give you a quicker comprehension of how the application should work.



    Type Safety


    Type safety lets the developer declare a variable with the instruction to only accept a value of a certain type, and nothing else. Like the following:

    type safety type safety

    In the above example only values that are string types can be assigned to stringVar, and only values that are a number can be assigned to numVar. Leaving out these type declarations can cause errors in your logic when the application is running, because a value of any kind can be assigned to it and make a method return an unfavourable result.

    type safety

    Type safety would also make debugging quicker and easier because assigning the wrong type of value to your variable will be picked up at compile time. Your code editor will also give you a very clear error message as to what the problem is, cutting down time to go and figure that out on your own.

    It is worth noting that Typescript has an Any type that allows for a value of any kind to be assigned to a variable. This type is not recommended and should only be used in edge cases, as this would be the same as not declaring your variable with a type at all!

    type safety



    Return Types


    Return types are in a way similar to type safety. The only difference is that you declare the type of data that should be returned by a method and not a variable. For example, if we write a method with a return type of string, then the only type of data that can be returned with it will have to be a string value, and nothing else.

    return types

    Once again, like type safety, the developer can understand what the purpose of a certain method is by looking at its return type.

    If there is no data returned by your method then the type Void is used. There is absolutely nothing wrong with using this as a return type, as a method can't always be expected to have a return value.

    return types

    Return types are also checked at compile time which means your debugging can be done more efficiently.

    return types

    Access modifiers, type safety and return types can seem trivial or time wasters at first, but once you start using these, they will feel like a necessary tool for you and your team members.

    Contact us


      Related posts:

      angularUpdating A Primitive Type Variable From A Service In Angular dynamic doughnut graph using laravel vue componentDynamic Doughnut Graph Using Laravel Vue Component laravel livewireLaravel Livewire laravel observersLaravel Observers
      Share
      0
      Werner Bestbier
      Werner Bestbier
      Werner is a front-end developer with a background in design. Making a users web-surfing life easy is his game. His preferred language/framework is Javascript and Angular.

      Leave a Reply Cancel reply

      Your email address will not be published. Required fields are marked *

      Lava Lamp Lab


      Like technology, a lava lamp constantly changes form, producing new conditions with every passing moment



      lava lamp lab facebook   lava lamp lab twitter   lava lamp lab linkedin   lava lamp lab instgram

      Services


      Application & software development

      Outsourced software development

      Project based resourcing

      Digital marketing & consulting

      Graphic design & consulting

      UI / UX design & consulting

      Contact Us


      +27(0) 83 419 4851

      +27(0) 21 036 1165

      +61(0) 42 420 8911


      switchon@lavalamp.biz


      Unit 1 Monaco Square,
      14 Church Street,
      Durbanville,
      Cape Town, 7550

      NBConsult Group


      nbconsult
      designer needed
      © 2023 Lava Lamp Lab (Pty) Ltd | All Rights Reserved | Privacy Policy
      Contact us now

        Application & software development

          Outsourced software development

            Project based resourcing

              Digital marketing & consulting

                Graphic design & consulting

                  UI/UX design & consulting

                    Lease an expert

                      Recruitment services

                        We are using cookies to give you the best experience on our website.

                        You can find out more about which cookies we are using or switch them off in settings.

                        Lava Lamp Lab
                        Powered by  GDPR Cookie Compliance
                        Privacy Overview

                        This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

                        Strictly Necessary Cookies

                        Strictly Necessary Cookie should be enabled at all times so that we can save your preferences for cookie settings.

                        If you disable this cookie, we will not be able to save your preferences. This means that every time you visit this website you will need to enable or disable cookies again.

                        3rd Party Cookies

                        This website uses Google Analytics to collect anonymous information such as the number of visitors to the site, and the most popular pages.

                        Keeping this cookie enabled helps us to improve our website.

                        Please enable Strictly Necessary Cookies first so that we can save your preferences!

                        Cookie Policy

                        More information about our Cookie Policy