Search code examples
javascripttypescriptbreezehottowel

Extending a breeze entity using TypeScript


I am developing a website using HotTowel and TypeScript. In John Papa's excellent PluralSight course, he extended a breezejs entity by creating a constructor and using 'Object.defineProperty' to extend it. For example, he added a property called fullName as follows.

NB: metadataStore is the breezejs metadata store

function registerPerson(metadataStore) {
        metadataStore.registerEntityTypeCtor('Person', Person);

        function Person() {
            this.isPartial = false;
            this.isSpeaker = false;
        }

        Object.defineProperty(Person.prototype, 'fullName', {
            get: function () {
                var fn = this.firstName;
                var ln = this.lastName;
                return ln ? fn + ' ' + ln : fn;
            }
        });
    }

My question is how would I achieve the same using TypeScript ?


Solution

  • Typescript support accessor out of the box. The TypeScript code you asked could be :

    function registerPerson(metadataStore) {
        metadataStore.registerEntityTypeCtor('Person', Person);
    }
    class Person {
        public firstName: string;
        public lastName: string;
    
        constructor(public isPartial=false, public isSpeaker=false) {
        }
    
        get fullName():string {
            return `${this.firstName} ${this.lastName}`;
        }
    }