“Computed” property in Typescript











up vote
0
down vote

favorite












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 at 12:25















up vote
0
down vote

favorite












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 at 12:25













up vote
0
down vote

favorite









up vote
0
down vote

favorite











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 at 12:19









marc_s

564k12510881241




564k12510881241












  • Have a look at the Accessors section here. I think a getter (get) might do what you need.
    – R. Richards
    Nov 10 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 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 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 at 12:25












3 Answers
3






active

oldest

votes

















up vote
2
down vote













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




























    up vote
    0
    down vote













    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




























      up vote
      0
      down vote













      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























        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',
        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
































        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        2
        down vote













        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

























          up vote
          2
          down vote













          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























            up vote
            2
            down vote










            up vote
            2
            down vote









            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 at 12:26









            Dai

            71.1k13111200




            71.1k13111200
























                up vote
                0
                down vote













                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

























                  up vote
                  0
                  down vote













                  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























                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote









                    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 at 12:28









                    Vivick

                    1,1951317




                    1,1951317






















                        up vote
                        0
                        down vote













                        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



























                          up vote
                          0
                          down vote













                          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

























                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            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 at 12:36

























                            answered Nov 10 at 12:26









                            SiddAjmera

                            9,01621037




                            9,01621037






























                                 

                                draft saved


                                draft discarded



















































                                 


                                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




















































































                                Popular posts from this blog

                                Full-time equivalent

                                さくらももこ

                                13 indicted, 8 arrested in Calif. drug cartel investigation