typescript optional property with a getter












2















This is a simplified example:



class PersonParms{
name:string;
lastName:string;
age?:number;
get fullName(){return this.name + " "+this.lastName;}
}

class Person{
constructor(prms:PersonParms){
}
}

new Person({name:'John',lastName:'Doe'}) // ts error: Property 'fullName' is missing in type '{ name: string; lastName: string; }'.


The idea is to pass a literal object as the intizalizer of PersonParms but having that getter you can neither declare the getter optional or add the property to the object literal. Is there another way to achieve it?










share|improve this question


















  • 2





    Consider defining an interface interface IPersonParms { name:string; lastName:string; age?:number; readonly fullName?: string; }. Casting object literal to class doesn't seem to be useful - getter won't magically appear there anyway, you'll need to create an instance of a PersonParms class.

    – Aleksey L.
    Feb 11 '18 at 6:47
















2















This is a simplified example:



class PersonParms{
name:string;
lastName:string;
age?:number;
get fullName(){return this.name + " "+this.lastName;}
}

class Person{
constructor(prms:PersonParms){
}
}

new Person({name:'John',lastName:'Doe'}) // ts error: Property 'fullName' is missing in type '{ name: string; lastName: string; }'.


The idea is to pass a literal object as the intizalizer of PersonParms but having that getter you can neither declare the getter optional or add the property to the object literal. Is there another way to achieve it?










share|improve this question


















  • 2





    Consider defining an interface interface IPersonParms { name:string; lastName:string; age?:number; readonly fullName?: string; }. Casting object literal to class doesn't seem to be useful - getter won't magically appear there anyway, you'll need to create an instance of a PersonParms class.

    – Aleksey L.
    Feb 11 '18 at 6:47














2












2








2


0






This is a simplified example:



class PersonParms{
name:string;
lastName:string;
age?:number;
get fullName(){return this.name + " "+this.lastName;}
}

class Person{
constructor(prms:PersonParms){
}
}

new Person({name:'John',lastName:'Doe'}) // ts error: Property 'fullName' is missing in type '{ name: string; lastName: string; }'.


The idea is to pass a literal object as the intizalizer of PersonParms but having that getter you can neither declare the getter optional or add the property to the object literal. Is there another way to achieve it?










share|improve this question














This is a simplified example:



class PersonParms{
name:string;
lastName:string;
age?:number;
get fullName(){return this.name + " "+this.lastName;}
}

class Person{
constructor(prms:PersonParms){
}
}

new Person({name:'John',lastName:'Doe'}) // ts error: Property 'fullName' is missing in type '{ name: string; lastName: string; }'.


The idea is to pass a literal object as the intizalizer of PersonParms but having that getter you can neither declare the getter optional or add the property to the object literal. Is there another way to achieve it?







typescript optional getter object-literal






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Feb 10 '18 at 21:48









tru7tru7

1,52811632




1,52811632








  • 2





    Consider defining an interface interface IPersonParms { name:string; lastName:string; age?:number; readonly fullName?: string; }. Casting object literal to class doesn't seem to be useful - getter won't magically appear there anyway, you'll need to create an instance of a PersonParms class.

    – Aleksey L.
    Feb 11 '18 at 6:47














  • 2





    Consider defining an interface interface IPersonParms { name:string; lastName:string; age?:number; readonly fullName?: string; }. Casting object literal to class doesn't seem to be useful - getter won't magically appear there anyway, you'll need to create an instance of a PersonParms class.

    – Aleksey L.
    Feb 11 '18 at 6:47








2




2





Consider defining an interface interface IPersonParms { name:string; lastName:string; age?:number; readonly fullName?: string; }. Casting object literal to class doesn't seem to be useful - getter won't magically appear there anyway, you'll need to create an instance of a PersonParms class.

– Aleksey L.
Feb 11 '18 at 6:47





Consider defining an interface interface IPersonParms { name:string; lastName:string; age?:number; readonly fullName?: string; }. Casting object literal to class doesn't seem to be useful - getter won't magically appear there anyway, you'll need to create an instance of a PersonParms class.

– Aleksey L.
Feb 11 '18 at 6:47












6 Answers
6






active

oldest

votes


















0














I found this solution which is ok for me:



class Person {
name?:string;
lastName?:string;
age?: number;
fullName?:string;

constructor(public config: { name: string, lastName: string }) {
Object.defineProperty(this,'fullName',{
get(){return this.name + " " + this.lastName;}
});

}





share|improve this answer

































    4














    Very interesting. I think, you should report an issue to TypeScript, because methods can be optional (see below), but property getters not. It is strange.. As a workaround I can suggest two variants. A nice one:



    class PersonParms {
    name:string;
    lastName:string;
    age?: number;

    getFullName?() {return this.name + " "+this.lastName;}
    }


    And a second one, that is hacky, because there we make all the properties optional when passing to constructor.



    class PersonParms {
    name:string;
    lastName:string;
    age?: number;

    get fullName(){return this.name + " "+this.lastName;}
    }

    class Person{
    constructor(prms: Partial<PersonParms>){
    }
    }





    share|improve this answer
























    • I second that. It is quite misleading to have to assign a value to a property that's (intentionally) read-only in the initialization - and that's effectively where the case described here takes us.

      – Wojtek
      Jul 19 '18 at 19:21





















    2















    Is there another way to achieve it?




    Here is how I would do it:



    class Person {
    constructor(public config: { name: string, lastName: string }) {}
    age?: number;
    get fullName() { return this.config.name + " " + this.config.lastName; }
    }

    new Person({ name: 'John', lastName: 'Doe' })





    share|improve this answer
























    • Yes but I will use PersonParms as base in several other classes so the optional getter should be in PersonParns to not have to repeat it in all the subclasses.

      – tru7
      Feb 10 '18 at 23:00



















    0














    If you creates a new instance of PersonParms then the error will be gone.



    class PersonParms{
    name:string;
    lastName:string;
    age?:number;
    get fullName(){return this.name + " "+this.lastName;}
    }

    class Person{
    constructor(prms:PersonParms){
    }
    }

    const personParams = new PersonParms();
    personParams.name = 'John';
    personParams.lastName = 'John';
    new Person(personParams) // No error because this is an instance of PersonParams


    I am not sure where/how do you use PersonParms.fullname but in your case I would use this:



    interface PersonParms{
    name:string;
    lastName:string;
    age?:number;
    }

    class Person implements PersonParms{
    name: string;
    lastName: string;
    age?:number
    constructor(prms: PersonParms) {
    this.name = prms.name;
    this.lastName = prms.lastName;
    this.age = prms.age;
    }

    get fullName(){return this.name + " "+this.lastName;}
    }

    const person = new Person({ name: 'John', lastName: 'Doe' });

    console.log(person.fullName); // John Doe





    share|improve this answer































      -1














      class PersonParms {
      name: string;
      lastName: string;
      age?: number;
      fullName?: string = this.name + ' ' + this.lastName;
      }

      class Person {
      constructor(prms: PersonParms) {
      }
      }

      new Person({ name: 'John', lastName: 'Doe' });





      share|improve this answer
























      • Hi Akshay, instead of just showing the code you should also explain what you've changed and why it solves the problem.

        – JJJ
        Nov 24 '18 at 7:14











      • Here I made only one change. fullName?: string = this.name + ' ' + this.lastName;

        – Akshay
        Nov 24 '18 at 7:23











      • It's working then why should I use getter for this?

        – Akshay
        Nov 24 '18 at 7:37





















      -1














         class PersonParms {
      name: string;
      lastName: string;
      age?: number;

      getFullName?(): string | null { return this.name + ' ' + this.lastName; }
      }

      class Person {
      constructor(prms: PersonParms) {
      }
      }
      new Person({ name: 'John', lastName: 'Doe' });





      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%2f48725916%2ftypescript-optional-property-with-a-getter%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        6 Answers
        6






        active

        oldest

        votes








        6 Answers
        6






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        0














        I found this solution which is ok for me:



        class Person {
        name?:string;
        lastName?:string;
        age?: number;
        fullName?:string;

        constructor(public config: { name: string, lastName: string }) {
        Object.defineProperty(this,'fullName',{
        get(){return this.name + " " + this.lastName;}
        });

        }





        share|improve this answer






























          0














          I found this solution which is ok for me:



          class Person {
          name?:string;
          lastName?:string;
          age?: number;
          fullName?:string;

          constructor(public config: { name: string, lastName: string }) {
          Object.defineProperty(this,'fullName',{
          get(){return this.name + " " + this.lastName;}
          });

          }





          share|improve this answer




























            0












            0








            0







            I found this solution which is ok for me:



            class Person {
            name?:string;
            lastName?:string;
            age?: number;
            fullName?:string;

            constructor(public config: { name: string, lastName: string }) {
            Object.defineProperty(this,'fullName',{
            get(){return this.name + " " + this.lastName;}
            });

            }





            share|improve this answer















            I found this solution which is ok for me:



            class Person {
            name?:string;
            lastName?:string;
            age?: number;
            fullName?:string;

            constructor(public config: { name: string, lastName: string }) {
            Object.defineProperty(this,'fullName',{
            get(){return this.name + " " + this.lastName;}
            });

            }






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 24 '18 at 8:07

























            answered Nov 24 '18 at 6:30









            BogdanBogdan

            151217




            151217

























                4














                Very interesting. I think, you should report an issue to TypeScript, because methods can be optional (see below), but property getters not. It is strange.. As a workaround I can suggest two variants. A nice one:



                class PersonParms {
                name:string;
                lastName:string;
                age?: number;

                getFullName?() {return this.name + " "+this.lastName;}
                }


                And a second one, that is hacky, because there we make all the properties optional when passing to constructor.



                class PersonParms {
                name:string;
                lastName:string;
                age?: number;

                get fullName(){return this.name + " "+this.lastName;}
                }

                class Person{
                constructor(prms: Partial<PersonParms>){
                }
                }





                share|improve this answer
























                • I second that. It is quite misleading to have to assign a value to a property that's (intentionally) read-only in the initialization - and that's effectively where the case described here takes us.

                  – Wojtek
                  Jul 19 '18 at 19:21


















                4














                Very interesting. I think, you should report an issue to TypeScript, because methods can be optional (see below), but property getters not. It is strange.. As a workaround I can suggest two variants. A nice one:



                class PersonParms {
                name:string;
                lastName:string;
                age?: number;

                getFullName?() {return this.name + " "+this.lastName;}
                }


                And a second one, that is hacky, because there we make all the properties optional when passing to constructor.



                class PersonParms {
                name:string;
                lastName:string;
                age?: number;

                get fullName(){return this.name + " "+this.lastName;}
                }

                class Person{
                constructor(prms: Partial<PersonParms>){
                }
                }





                share|improve this answer
























                • I second that. It is quite misleading to have to assign a value to a property that's (intentionally) read-only in the initialization - and that's effectively where the case described here takes us.

                  – Wojtek
                  Jul 19 '18 at 19:21
















                4












                4








                4







                Very interesting. I think, you should report an issue to TypeScript, because methods can be optional (see below), but property getters not. It is strange.. As a workaround I can suggest two variants. A nice one:



                class PersonParms {
                name:string;
                lastName:string;
                age?: number;

                getFullName?() {return this.name + " "+this.lastName;}
                }


                And a second one, that is hacky, because there we make all the properties optional when passing to constructor.



                class PersonParms {
                name:string;
                lastName:string;
                age?: number;

                get fullName(){return this.name + " "+this.lastName;}
                }

                class Person{
                constructor(prms: Partial<PersonParms>){
                }
                }





                share|improve this answer













                Very interesting. I think, you should report an issue to TypeScript, because methods can be optional (see below), but property getters not. It is strange.. As a workaround I can suggest two variants. A nice one:



                class PersonParms {
                name:string;
                lastName:string;
                age?: number;

                getFullName?() {return this.name + " "+this.lastName;}
                }


                And a second one, that is hacky, because there we make all the properties optional when passing to constructor.



                class PersonParms {
                name:string;
                lastName:string;
                age?: number;

                get fullName(){return this.name + " "+this.lastName;}
                }

                class Person{
                constructor(prms: Partial<PersonParms>){
                }
                }






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Feb 11 '18 at 8:03









                PavelPavel

                1,5271022




                1,5271022













                • I second that. It is quite misleading to have to assign a value to a property that's (intentionally) read-only in the initialization - and that's effectively where the case described here takes us.

                  – Wojtek
                  Jul 19 '18 at 19:21





















                • I second that. It is quite misleading to have to assign a value to a property that's (intentionally) read-only in the initialization - and that's effectively where the case described here takes us.

                  – Wojtek
                  Jul 19 '18 at 19:21



















                I second that. It is quite misleading to have to assign a value to a property that's (intentionally) read-only in the initialization - and that's effectively where the case described here takes us.

                – Wojtek
                Jul 19 '18 at 19:21







                I second that. It is quite misleading to have to assign a value to a property that's (intentionally) read-only in the initialization - and that's effectively where the case described here takes us.

                – Wojtek
                Jul 19 '18 at 19:21













                2















                Is there another way to achieve it?




                Here is how I would do it:



                class Person {
                constructor(public config: { name: string, lastName: string }) {}
                age?: number;
                get fullName() { return this.config.name + " " + this.config.lastName; }
                }

                new Person({ name: 'John', lastName: 'Doe' })





                share|improve this answer
























                • Yes but I will use PersonParms as base in several other classes so the optional getter should be in PersonParns to not have to repeat it in all the subclasses.

                  – tru7
                  Feb 10 '18 at 23:00
















                2















                Is there another way to achieve it?




                Here is how I would do it:



                class Person {
                constructor(public config: { name: string, lastName: string }) {}
                age?: number;
                get fullName() { return this.config.name + " " + this.config.lastName; }
                }

                new Person({ name: 'John', lastName: 'Doe' })





                share|improve this answer
























                • Yes but I will use PersonParms as base in several other classes so the optional getter should be in PersonParns to not have to repeat it in all the subclasses.

                  – tru7
                  Feb 10 '18 at 23:00














                2












                2








                2








                Is there another way to achieve it?




                Here is how I would do it:



                class Person {
                constructor(public config: { name: string, lastName: string }) {}
                age?: number;
                get fullName() { return this.config.name + " " + this.config.lastName; }
                }

                new Person({ name: 'John', lastName: 'Doe' })





                share|improve this answer














                Is there another way to achieve it?




                Here is how I would do it:



                class Person {
                constructor(public config: { name: string, lastName: string }) {}
                age?: number;
                get fullName() { return this.config.name + " " + this.config.lastName; }
                }

                new Person({ name: 'John', lastName: 'Doe' })






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Feb 10 '18 at 21:58









                basaratbasarat

                138k25255363




                138k25255363













                • Yes but I will use PersonParms as base in several other classes so the optional getter should be in PersonParns to not have to repeat it in all the subclasses.

                  – tru7
                  Feb 10 '18 at 23:00



















                • Yes but I will use PersonParms as base in several other classes so the optional getter should be in PersonParns to not have to repeat it in all the subclasses.

                  – tru7
                  Feb 10 '18 at 23:00

















                Yes but I will use PersonParms as base in several other classes so the optional getter should be in PersonParns to not have to repeat it in all the subclasses.

                – tru7
                Feb 10 '18 at 23:00





                Yes but I will use PersonParms as base in several other classes so the optional getter should be in PersonParns to not have to repeat it in all the subclasses.

                – tru7
                Feb 10 '18 at 23:00











                0














                If you creates a new instance of PersonParms then the error will be gone.



                class PersonParms{
                name:string;
                lastName:string;
                age?:number;
                get fullName(){return this.name + " "+this.lastName;}
                }

                class Person{
                constructor(prms:PersonParms){
                }
                }

                const personParams = new PersonParms();
                personParams.name = 'John';
                personParams.lastName = 'John';
                new Person(personParams) // No error because this is an instance of PersonParams


                I am not sure where/how do you use PersonParms.fullname but in your case I would use this:



                interface PersonParms{
                name:string;
                lastName:string;
                age?:number;
                }

                class Person implements PersonParms{
                name: string;
                lastName: string;
                age?:number
                constructor(prms: PersonParms) {
                this.name = prms.name;
                this.lastName = prms.lastName;
                this.age = prms.age;
                }

                get fullName(){return this.name + " "+this.lastName;}
                }

                const person = new Person({ name: 'John', lastName: 'Doe' });

                console.log(person.fullName); // John Doe





                share|improve this answer




























                  0














                  If you creates a new instance of PersonParms then the error will be gone.



                  class PersonParms{
                  name:string;
                  lastName:string;
                  age?:number;
                  get fullName(){return this.name + " "+this.lastName;}
                  }

                  class Person{
                  constructor(prms:PersonParms){
                  }
                  }

                  const personParams = new PersonParms();
                  personParams.name = 'John';
                  personParams.lastName = 'John';
                  new Person(personParams) // No error because this is an instance of PersonParams


                  I am not sure where/how do you use PersonParms.fullname but in your case I would use this:



                  interface PersonParms{
                  name:string;
                  lastName:string;
                  age?:number;
                  }

                  class Person implements PersonParms{
                  name: string;
                  lastName: string;
                  age?:number
                  constructor(prms: PersonParms) {
                  this.name = prms.name;
                  this.lastName = prms.lastName;
                  this.age = prms.age;
                  }

                  get fullName(){return this.name + " "+this.lastName;}
                  }

                  const person = new Person({ name: 'John', lastName: 'Doe' });

                  console.log(person.fullName); // John Doe





                  share|improve this answer


























                    0












                    0








                    0







                    If you creates a new instance of PersonParms then the error will be gone.



                    class PersonParms{
                    name:string;
                    lastName:string;
                    age?:number;
                    get fullName(){return this.name + " "+this.lastName;}
                    }

                    class Person{
                    constructor(prms:PersonParms){
                    }
                    }

                    const personParams = new PersonParms();
                    personParams.name = 'John';
                    personParams.lastName = 'John';
                    new Person(personParams) // No error because this is an instance of PersonParams


                    I am not sure where/how do you use PersonParms.fullname but in your case I would use this:



                    interface PersonParms{
                    name:string;
                    lastName:string;
                    age?:number;
                    }

                    class Person implements PersonParms{
                    name: string;
                    lastName: string;
                    age?:number
                    constructor(prms: PersonParms) {
                    this.name = prms.name;
                    this.lastName = prms.lastName;
                    this.age = prms.age;
                    }

                    get fullName(){return this.name + " "+this.lastName;}
                    }

                    const person = new Person({ name: 'John', lastName: 'Doe' });

                    console.log(person.fullName); // John Doe





                    share|improve this answer













                    If you creates a new instance of PersonParms then the error will be gone.



                    class PersonParms{
                    name:string;
                    lastName:string;
                    age?:number;
                    get fullName(){return this.name + " "+this.lastName;}
                    }

                    class Person{
                    constructor(prms:PersonParms){
                    }
                    }

                    const personParams = new PersonParms();
                    personParams.name = 'John';
                    personParams.lastName = 'John';
                    new Person(personParams) // No error because this is an instance of PersonParams


                    I am not sure where/how do you use PersonParms.fullname but in your case I would use this:



                    interface PersonParms{
                    name:string;
                    lastName:string;
                    age?:number;
                    }

                    class Person implements PersonParms{
                    name: string;
                    lastName: string;
                    age?:number
                    constructor(prms: PersonParms) {
                    this.name = prms.name;
                    this.lastName = prms.lastName;
                    this.age = prms.age;
                    }

                    get fullName(){return this.name + " "+this.lastName;}
                    }

                    const person = new Person({ name: 'John', lastName: 'Doe' });

                    console.log(person.fullName); // John Doe






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jan 19 at 20:52









                    distantedistante

                    1,0841434




                    1,0841434























                        -1














                        class PersonParms {
                        name: string;
                        lastName: string;
                        age?: number;
                        fullName?: string = this.name + ' ' + this.lastName;
                        }

                        class Person {
                        constructor(prms: PersonParms) {
                        }
                        }

                        new Person({ name: 'John', lastName: 'Doe' });





                        share|improve this answer
























                        • Hi Akshay, instead of just showing the code you should also explain what you've changed and why it solves the problem.

                          – JJJ
                          Nov 24 '18 at 7:14











                        • Here I made only one change. fullName?: string = this.name + ' ' + this.lastName;

                          – Akshay
                          Nov 24 '18 at 7:23











                        • It's working then why should I use getter for this?

                          – Akshay
                          Nov 24 '18 at 7:37


















                        -1














                        class PersonParms {
                        name: string;
                        lastName: string;
                        age?: number;
                        fullName?: string = this.name + ' ' + this.lastName;
                        }

                        class Person {
                        constructor(prms: PersonParms) {
                        }
                        }

                        new Person({ name: 'John', lastName: 'Doe' });





                        share|improve this answer
























                        • Hi Akshay, instead of just showing the code you should also explain what you've changed and why it solves the problem.

                          – JJJ
                          Nov 24 '18 at 7:14











                        • Here I made only one change. fullName?: string = this.name + ' ' + this.lastName;

                          – Akshay
                          Nov 24 '18 at 7:23











                        • It's working then why should I use getter for this?

                          – Akshay
                          Nov 24 '18 at 7:37
















                        -1












                        -1








                        -1







                        class PersonParms {
                        name: string;
                        lastName: string;
                        age?: number;
                        fullName?: string = this.name + ' ' + this.lastName;
                        }

                        class Person {
                        constructor(prms: PersonParms) {
                        }
                        }

                        new Person({ name: 'John', lastName: 'Doe' });





                        share|improve this answer













                        class PersonParms {
                        name: string;
                        lastName: string;
                        age?: number;
                        fullName?: string = this.name + ' ' + this.lastName;
                        }

                        class Person {
                        constructor(prms: PersonParms) {
                        }
                        }

                        new Person({ name: 'John', lastName: 'Doe' });






                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Nov 24 '18 at 6:51









                        AkshayAkshay

                        362




                        362













                        • Hi Akshay, instead of just showing the code you should also explain what you've changed and why it solves the problem.

                          – JJJ
                          Nov 24 '18 at 7:14











                        • Here I made only one change. fullName?: string = this.name + ' ' + this.lastName;

                          – Akshay
                          Nov 24 '18 at 7:23











                        • It's working then why should I use getter for this?

                          – Akshay
                          Nov 24 '18 at 7:37





















                        • Hi Akshay, instead of just showing the code you should also explain what you've changed and why it solves the problem.

                          – JJJ
                          Nov 24 '18 at 7:14











                        • Here I made only one change. fullName?: string = this.name + ' ' + this.lastName;

                          – Akshay
                          Nov 24 '18 at 7:23











                        • It's working then why should I use getter for this?

                          – Akshay
                          Nov 24 '18 at 7:37



















                        Hi Akshay, instead of just showing the code you should also explain what you've changed and why it solves the problem.

                        – JJJ
                        Nov 24 '18 at 7:14





                        Hi Akshay, instead of just showing the code you should also explain what you've changed and why it solves the problem.

                        – JJJ
                        Nov 24 '18 at 7:14













                        Here I made only one change. fullName?: string = this.name + ' ' + this.lastName;

                        – Akshay
                        Nov 24 '18 at 7:23





                        Here I made only one change. fullName?: string = this.name + ' ' + this.lastName;

                        – Akshay
                        Nov 24 '18 at 7:23













                        It's working then why should I use getter for this?

                        – Akshay
                        Nov 24 '18 at 7:37







                        It's working then why should I use getter for this?

                        – Akshay
                        Nov 24 '18 at 7:37













                        -1














                           class PersonParms {
                        name: string;
                        lastName: string;
                        age?: number;

                        getFullName?(): string | null { return this.name + ' ' + this.lastName; }
                        }

                        class Person {
                        constructor(prms: PersonParms) {
                        }
                        }
                        new Person({ name: 'John', lastName: 'Doe' });





                        share|improve this answer




























                          -1














                             class PersonParms {
                          name: string;
                          lastName: string;
                          age?: number;

                          getFullName?(): string | null { return this.name + ' ' + this.lastName; }
                          }

                          class Person {
                          constructor(prms: PersonParms) {
                          }
                          }
                          new Person({ name: 'John', lastName: 'Doe' });





                          share|improve this answer


























                            -1












                            -1








                            -1







                               class PersonParms {
                            name: string;
                            lastName: string;
                            age?: number;

                            getFullName?(): string | null { return this.name + ' ' + this.lastName; }
                            }

                            class Person {
                            constructor(prms: PersonParms) {
                            }
                            }
                            new Person({ name: 'John', lastName: 'Doe' });





                            share|improve this answer













                               class PersonParms {
                            name: string;
                            lastName: string;
                            age?: number;

                            getFullName?(): string | null { return this.name + ' ' + this.lastName; }
                            }

                            class Person {
                            constructor(prms: PersonParms) {
                            }
                            }
                            new Person({ name: 'John', lastName: 'Doe' });






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 24 '18 at 7:39









                            AkshayAkshay

                            362




                            362






























                                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.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f48725916%2ftypescript-optional-property-with-a-getter%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

                                404 Error Contact Form 7 ajax form submitting

                                How to know if a Active Directory user can login interactively

                                TypeError: fit_transform() missing 1 required positional argument: 'X'