“Computed” property in Typescript










1














Folks, I'm in the middle of "learning by doing" in Angular 6 & 7, and I came across this issue a number of times.



Imagine I have a class/interface/type - e.g. Person - with a few properties, that are returned by a Web API call - something like this:



export interface Person 
FirstName: string;
LastName: string;
JobTitle: string;
// so other properties - not relevant to this question



What I'd like to be able to is show the full name (e.g. "FirstName + [Space] + LastName") in e.g. an Angular grid (AG-Grid) or someplace else - where I cannot use a concat expression or anything, but I need to refer to a single property on the class/interface/type.



In C#, I would just create a property



string FullName get return $"FirstName LastName"; 


and be done with it - but how can I do this in Typescript?? From what I've been reading and researching, this seems to be unsupported - really?!?!!? How can that be??? Seems like such a simple and often-used operation - what's the reason this doesn't exist in Typescript?? Or does it exist - and I just haven't found the way to do this in TS?










share|improve this question





















  • Have a look at the Accessors section here. I think a getter (get) might do what you need.
    – R. Richards
    Nov 10 '18 at 12:25















1














Folks, I'm in the middle of "learning by doing" in Angular 6 & 7, and I came across this issue a number of times.



Imagine I have a class/interface/type - e.g. Person - with a few properties, that are returned by a Web API call - something like this:



export interface Person 
FirstName: string;
LastName: string;
JobTitle: string;
// so other properties - not relevant to this question



What I'd like to be able to is show the full name (e.g. "FirstName + [Space] + LastName") in e.g. an Angular grid (AG-Grid) or someplace else - where I cannot use a concat expression or anything, but I need to refer to a single property on the class/interface/type.



In C#, I would just create a property



string FullName get return $"FirstName LastName"; 


and be done with it - but how can I do this in Typescript?? From what I've been reading and researching, this seems to be unsupported - really?!?!!? How can that be??? Seems like such a simple and often-used operation - what's the reason this doesn't exist in Typescript?? Or does it exist - and I just haven't found the way to do this in TS?










share|improve this question





















  • Have a look at the Accessors section here. I think a getter (get) might do what you need.
    – R. Richards
    Nov 10 '18 at 12:25













1












1








1







Folks, I'm in the middle of "learning by doing" in Angular 6 & 7, and I came across this issue a number of times.



Imagine I have a class/interface/type - e.g. Person - with a few properties, that are returned by a Web API call - something like this:



export interface Person 
FirstName: string;
LastName: string;
JobTitle: string;
// so other properties - not relevant to this question



What I'd like to be able to is show the full name (e.g. "FirstName + [Space] + LastName") in e.g. an Angular grid (AG-Grid) or someplace else - where I cannot use a concat expression or anything, but I need to refer to a single property on the class/interface/type.



In C#, I would just create a property



string FullName get return $"FirstName LastName"; 


and be done with it - but how can I do this in Typescript?? From what I've been reading and researching, this seems to be unsupported - really?!?!!? How can that be??? Seems like such a simple and often-used operation - what's the reason this doesn't exist in Typescript?? Or does it exist - and I just haven't found the way to do this in TS?










share|improve this question













Folks, I'm in the middle of "learning by doing" in Angular 6 & 7, and I came across this issue a number of times.



Imagine I have a class/interface/type - e.g. Person - with a few properties, that are returned by a Web API call - something like this:



export interface Person 
FirstName: string;
LastName: string;
JobTitle: string;
// so other properties - not relevant to this question



What I'd like to be able to is show the full name (e.g. "FirstName + [Space] + LastName") in e.g. an Angular grid (AG-Grid) or someplace else - where I cannot use a concat expression or anything, but I need to refer to a single property on the class/interface/type.



In C#, I would just create a property



string FullName get return $"FirstName LastName"; 


and be done with it - but how can I do this in Typescript?? From what I've been reading and researching, this seems to be unsupported - really?!?!!? How can that be??? Seems like such a simple and often-used operation - what's the reason this doesn't exist in Typescript?? Or does it exist - and I just haven't found the way to do this in TS?







c# angular typescript properties






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 10 '18 at 12:19









marc_s

571k12811031252




571k12811031252











  • Have a look at the Accessors section here. I think a getter (get) might do what you need.
    – R. Richards
    Nov 10 '18 at 12:25
















  • Have a look at the Accessors section here. I think a getter (get) might do what you need.
    – R. Richards
    Nov 10 '18 at 12:25















Have a look at the Accessors section here. I think a getter (get) might do what you need.
– R. Richards
Nov 10 '18 at 12:25




Have a look at the Accessors section here. I think a getter (get) might do what you need.
– R. Richards
Nov 10 '18 at 12:25












4 Answers
4






active

oldest

votes


















3














If it's an interface then there's no syntax, because all properties in JavaScript can have getter/setter functions instead of being exposed fields. It's an implementation concern.



BTW members in TypeScript use camelCase:



export interface Person 
firstName: string;
lastName : string;
jobTitle : string;

fullName : string;


class SimplePerson implements Person

// ...

get fullName(): string
return this.firstName + " " + this.lastName;







share|improve this answer




























    1














    Javascript supports get and set when defining a property (mostly using Object.defineProperty).



    Apparently there's an handy syntax for it in typescript (for classes) :



    class MyClass
    firstName: string;
    lastName: string;

    constructor(firstName: string, lastName: string)
    this.firstName = firstName;
    this.lastName = lastName;


    get fullName()
    return `$this.firstName $this.lastName`;




    Regarding interfaces, I'm not sure such a thing is possible since their sole purpose is data description not implementation.






    share|improve this answer




























      1














      You can also define getters and setters in JavaScript.



      Try this in your Component Class:



      person: Person;
      ...
      // You got the person Object from your Backend API.
      ...
      // Now
      get fullName()
      return `$this.person.firstName $this.person.lastName`;



      And then in your Template:



      Simply use fullName like this:



      <p> fullName </p>





      share|improve this answer






























        0














        Could try using an array of string;



        var fullName: Array<string> = Array<string>();
        var firstName: string = 'Bob';
        var lastName: string = 'Boberanne';
        fullName.push(firstName);
        fullName.push(lastName);


        In html:



        <p>fullName.join(' ')</p>
        <p>[fistname, lastName].join(' ')</p>


        In typescript:



        fullName.join(' ');
        [firstName, lastName].join(' ');





        share|improve this answer




















          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238881%2fcomputed-property-in-typescript%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          4 Answers
          4






          active

          oldest

          votes








          4 Answers
          4






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          3














          If it's an interface then there's no syntax, because all properties in JavaScript can have getter/setter functions instead of being exposed fields. It's an implementation concern.



          BTW members in TypeScript use camelCase:



          export interface Person 
          firstName: string;
          lastName : string;
          jobTitle : string;

          fullName : string;


          class SimplePerson implements Person

          // ...

          get fullName(): string
          return this.firstName + " " + this.lastName;







          share|improve this answer

























            3














            If it's an interface then there's no syntax, because all properties in JavaScript can have getter/setter functions instead of being exposed fields. It's an implementation concern.



            BTW members in TypeScript use camelCase:



            export interface Person 
            firstName: string;
            lastName : string;
            jobTitle : string;

            fullName : string;


            class SimplePerson implements Person

            // ...

            get fullName(): string
            return this.firstName + " " + this.lastName;







            share|improve this answer























              3












              3








              3






              If it's an interface then there's no syntax, because all properties in JavaScript can have getter/setter functions instead of being exposed fields. It's an implementation concern.



              BTW members in TypeScript use camelCase:



              export interface Person 
              firstName: string;
              lastName : string;
              jobTitle : string;

              fullName : string;


              class SimplePerson implements Person

              // ...

              get fullName(): string
              return this.firstName + " " + this.lastName;







              share|improve this answer












              If it's an interface then there's no syntax, because all properties in JavaScript can have getter/setter functions instead of being exposed fields. It's an implementation concern.



              BTW members in TypeScript use camelCase:



              export interface Person 
              firstName: string;
              lastName : string;
              jobTitle : string;

              fullName : string;


              class SimplePerson implements Person

              // ...

              get fullName(): string
              return this.firstName + " " + this.lastName;








              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 10 '18 at 12:26









              Dai

              72k13113201




              72k13113201























                  1














                  Javascript supports get and set when defining a property (mostly using Object.defineProperty).



                  Apparently there's an handy syntax for it in typescript (for classes) :



                  class MyClass
                  firstName: string;
                  lastName: string;

                  constructor(firstName: string, lastName: string)
                  this.firstName = firstName;
                  this.lastName = lastName;


                  get fullName()
                  return `$this.firstName $this.lastName`;




                  Regarding interfaces, I'm not sure such a thing is possible since their sole purpose is data description not implementation.






                  share|improve this answer

























                    1














                    Javascript supports get and set when defining a property (mostly using Object.defineProperty).



                    Apparently there's an handy syntax for it in typescript (for classes) :



                    class MyClass
                    firstName: string;
                    lastName: string;

                    constructor(firstName: string, lastName: string)
                    this.firstName = firstName;
                    this.lastName = lastName;


                    get fullName()
                    return `$this.firstName $this.lastName`;




                    Regarding interfaces, I'm not sure such a thing is possible since their sole purpose is data description not implementation.






                    share|improve this answer























                      1












                      1








                      1






                      Javascript supports get and set when defining a property (mostly using Object.defineProperty).



                      Apparently there's an handy syntax for it in typescript (for classes) :



                      class MyClass
                      firstName: string;
                      lastName: string;

                      constructor(firstName: string, lastName: string)
                      this.firstName = firstName;
                      this.lastName = lastName;


                      get fullName()
                      return `$this.firstName $this.lastName`;




                      Regarding interfaces, I'm not sure such a thing is possible since their sole purpose is data description not implementation.






                      share|improve this answer












                      Javascript supports get and set when defining a property (mostly using Object.defineProperty).



                      Apparently there's an handy syntax for it in typescript (for classes) :



                      class MyClass
                      firstName: string;
                      lastName: string;

                      constructor(firstName: string, lastName: string)
                      this.firstName = firstName;
                      this.lastName = lastName;


                      get fullName()
                      return `$this.firstName $this.lastName`;




                      Regarding interfaces, I'm not sure such a thing is possible since their sole purpose is data description not implementation.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 10 '18 at 12:28









                      Vivick

                      1,3411417




                      1,3411417





















                          1














                          You can also define getters and setters in JavaScript.



                          Try this in your Component Class:



                          person: Person;
                          ...
                          // You got the person Object from your Backend API.
                          ...
                          // Now
                          get fullName()
                          return `$this.person.firstName $this.person.lastName`;



                          And then in your Template:



                          Simply use fullName like this:



                          <p> fullName </p>





                          share|improve this answer



























                            1














                            You can also define getters and setters in JavaScript.



                            Try this in your Component Class:



                            person: Person;
                            ...
                            // You got the person Object from your Backend API.
                            ...
                            // Now
                            get fullName()
                            return `$this.person.firstName $this.person.lastName`;



                            And then in your Template:



                            Simply use fullName like this:



                            <p> fullName </p>





                            share|improve this answer

























                              1












                              1








                              1






                              You can also define getters and setters in JavaScript.



                              Try this in your Component Class:



                              person: Person;
                              ...
                              // You got the person Object from your Backend API.
                              ...
                              // Now
                              get fullName()
                              return `$this.person.firstName $this.person.lastName`;



                              And then in your Template:



                              Simply use fullName like this:



                              <p> fullName </p>





                              share|improve this answer














                              You can also define getters and setters in JavaScript.



                              Try this in your Component Class:



                              person: Person;
                              ...
                              // You got the person Object from your Backend API.
                              ...
                              // Now
                              get fullName()
                              return `$this.person.firstName $this.person.lastName`;



                              And then in your Template:



                              Simply use fullName like this:



                              <p> fullName </p>






                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Nov 10 '18 at 12:36

























                              answered Nov 10 '18 at 12:26









                              SiddAjmera

                              13.1k31137




                              13.1k31137





















                                  0














                                  Could try using an array of string;



                                  var fullName: Array<string> = Array<string>();
                                  var firstName: string = 'Bob';
                                  var lastName: string = 'Boberanne';
                                  fullName.push(firstName);
                                  fullName.push(lastName);


                                  In html:



                                  <p>fullName.join(' ')</p>
                                  <p>[fistname, lastName].join(' ')</p>


                                  In typescript:



                                  fullName.join(' ');
                                  [firstName, lastName].join(' ');





                                  share|improve this answer

























                                    0














                                    Could try using an array of string;



                                    var fullName: Array<string> = Array<string>();
                                    var firstName: string = 'Bob';
                                    var lastName: string = 'Boberanne';
                                    fullName.push(firstName);
                                    fullName.push(lastName);


                                    In html:



                                    <p>fullName.join(' ')</p>
                                    <p>[fistname, lastName].join(' ')</p>


                                    In typescript:



                                    fullName.join(' ');
                                    [firstName, lastName].join(' ');





                                    share|improve this answer























                                      0












                                      0








                                      0






                                      Could try using an array of string;



                                      var fullName: Array<string> = Array<string>();
                                      var firstName: string = 'Bob';
                                      var lastName: string = 'Boberanne';
                                      fullName.push(firstName);
                                      fullName.push(lastName);


                                      In html:



                                      <p>fullName.join(' ')</p>
                                      <p>[fistname, lastName].join(' ')</p>


                                      In typescript:



                                      fullName.join(' ');
                                      [firstName, lastName].join(' ');





                                      share|improve this answer












                                      Could try using an array of string;



                                      var fullName: Array<string> = Array<string>();
                                      var firstName: string = 'Bob';
                                      var lastName: string = 'Boberanne';
                                      fullName.push(firstName);
                                      fullName.push(lastName);


                                      In html:



                                      <p>fullName.join(' ')</p>
                                      <p>[fistname, lastName].join(' ')</p>


                                      In typescript:



                                      fullName.join(' ');
                                      [firstName, lastName].join(' ');






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Dec 26 '18 at 21:02









                                      RetroCoder

                                      1,25094272




                                      1,25094272



























                                          draft saved

                                          draft discarded
















































                                          Thanks for contributing an answer to Stack Overflow!


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid


                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.

                                          To learn more, see our tips on writing great answers.





                                          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                          Please pay close attention to the following guidance:


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid


                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.

                                          To learn more, see our tips on writing great answers.




                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function ()
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238881%2fcomputed-property-in-typescript%23new-answer', 'question_page');

                                          );

                                          Post as a guest















                                          Required, but never shown





















































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown

































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown







                                          Popular posts from this blog

                                          𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

                                          Edmonton

                                          Crossroads (UK TV series)