TypeScript Property Decorators The Next CEO of Stack OverflowHow to make a chain of function decorators?Decorators with parameters?What is TypeScript and why would I use it in place of JavaScript?How do you explicitly set a new property on `window` in TypeScript?get and set in TypeScriptTypeScript Converting a String to a numberHow does the @property decorator work?How to implement a typescript decorator?Can't bind to 'ngModel' since it isn't a known property of 'input'Typescript / Javascript custom Property decorators

Defamation due to breach of confidentiality

Is French Guiana a (hard) EU border?

Is there a way to save my career from absolute disaster?

Film where the government was corrupt with aliens, people sent to kill aliens are given rigged visors not showing the right aliens

Is it convenient to ask the journal's editor for two additional days to complete a review?

(How) Could a medieval fantasy world survive a magic-induced "nuclear winter"?

What does "shotgun unity" refer to here in this sentence?

What is the process for purifying your home if you believe it may have been previously used for pagan worship?

Do I need to write [sic] when including a quotation with a number less than 10 that isn't written out?

Audio Conversion With ADS1243

From jafe to El-Guest

Is fine stranded wire ok for main supply line?

Is dried pee considered dirt?

What difference does it make using sed with/without whitespaces?

Which Pokemon have a special animation when running with them out of their pokeball?

Small nick on power cord from an electric alarm clock, and copper wiring exposed but intact

If Nick Fury and Coulson already knew about aliens (Kree and Skrull) why did they wait until Thor's appearance to start making weapons?

Is it okay to majorly distort historical facts while writing a fiction story?

IC has pull-down resistors on SMBus lines?

Expressing the idea of having a very busy time

Is there a difference between "Fahrstuhl" and "Aufzug"?

Does higher Oxidation/ reduction potential translate to higher energy storage in battery?

Can this note be analyzed as a non-chord tone?

Why is information "lost" when it got into a black hole?



TypeScript Property Decorators



The Next CEO of Stack OverflowHow to make a chain of function decorators?Decorators with parameters?What is TypeScript and why would I use it in place of JavaScript?How do you explicitly set a new property on `window` in TypeScript?get and set in TypeScriptTypeScript Converting a String to a numberHow does the @property decorator work?How to implement a typescript decorator?Can't bind to 'ngModel' since it isn't a known property of 'input'Typescript / Javascript custom Property decorators










0















I'm looking to isolate model validation within the model class itself, without having to hard-code required states (our designer guys don't know what is required, just that they need to hook up properties). I'm trying the property decorator path without much luck. The constructor overwrites what the decorator sets. Is this even possible, should I go about it another way?



model.ts



@Required
name: string;

hobbies: string;

constructor(dto?: any)
this.name = dto.name; // This clears my "required" property
this.hobbies = dto.hobbies;



decorator.ts



function Required(target: any, key: string) 
Object.defineProperty(target, "required", value: true );



What I am hoping to accomplish...



*.html



<input name="name" [(ngModel)]="model.name" [required]="model.name.hasOwnProperty('required')">


I've also tried playing with the String prototype itself to initialize this property, but the constructor still clears anything that gets set.



String.prototype["required"] = false;









share|improve this question






















  • I am not following how you intend to use/create this. Could you create an Minimal, Complete, and Verifiable example on stackblitz.com? I realize the code won't work but it will convey the intention.

    – Igor
    Mar 7 at 18:32















0















I'm looking to isolate model validation within the model class itself, without having to hard-code required states (our designer guys don't know what is required, just that they need to hook up properties). I'm trying the property decorator path without much luck. The constructor overwrites what the decorator sets. Is this even possible, should I go about it another way?



model.ts



@Required
name: string;

hobbies: string;

constructor(dto?: any)
this.name = dto.name; // This clears my "required" property
this.hobbies = dto.hobbies;



decorator.ts



function Required(target: any, key: string) 
Object.defineProperty(target, "required", value: true );



What I am hoping to accomplish...



*.html



<input name="name" [(ngModel)]="model.name" [required]="model.name.hasOwnProperty('required')">


I've also tried playing with the String prototype itself to initialize this property, but the constructor still clears anything that gets set.



String.prototype["required"] = false;









share|improve this question






















  • I am not following how you intend to use/create this. Could you create an Minimal, Complete, and Verifiable example on stackblitz.com? I realize the code won't work but it will convey the intention.

    – Igor
    Mar 7 at 18:32













0












0








0








I'm looking to isolate model validation within the model class itself, without having to hard-code required states (our designer guys don't know what is required, just that they need to hook up properties). I'm trying the property decorator path without much luck. The constructor overwrites what the decorator sets. Is this even possible, should I go about it another way?



model.ts



@Required
name: string;

hobbies: string;

constructor(dto?: any)
this.name = dto.name; // This clears my "required" property
this.hobbies = dto.hobbies;



decorator.ts



function Required(target: any, key: string) 
Object.defineProperty(target, "required", value: true );



What I am hoping to accomplish...



*.html



<input name="name" [(ngModel)]="model.name" [required]="model.name.hasOwnProperty('required')">


I've also tried playing with the String prototype itself to initialize this property, but the constructor still clears anything that gets set.



String.prototype["required"] = false;









share|improve this question














I'm looking to isolate model validation within the model class itself, without having to hard-code required states (our designer guys don't know what is required, just that they need to hook up properties). I'm trying the property decorator path without much luck. The constructor overwrites what the decorator sets. Is this even possible, should I go about it another way?



model.ts



@Required
name: string;

hobbies: string;

constructor(dto?: any)
this.name = dto.name; // This clears my "required" property
this.hobbies = dto.hobbies;



decorator.ts



function Required(target: any, key: string) 
Object.defineProperty(target, "required", value: true );



What I am hoping to accomplish...



*.html



<input name="name" [(ngModel)]="model.name" [required]="model.name.hasOwnProperty('required')">


I've also tried playing with the String prototype itself to initialize this property, but the constructor still clears anything that gets set.



String.prototype["required"] = false;






angular typescript validation decorator






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 7 at 18:30









DanielleDanielle

235214




235214












  • I am not following how you intend to use/create this. Could you create an Minimal, Complete, and Verifiable example on stackblitz.com? I realize the code won't work but it will convey the intention.

    – Igor
    Mar 7 at 18:32

















  • I am not following how you intend to use/create this. Could you create an Minimal, Complete, and Verifiable example on stackblitz.com? I realize the code won't work but it will convey the intention.

    – Igor
    Mar 7 at 18:32
















I am not following how you intend to use/create this. Could you create an Minimal, Complete, and Verifiable example on stackblitz.com? I realize the code won't work but it will convey the intention.

– Igor
Mar 7 at 18:32





I am not following how you intend to use/create this. Could you create an Minimal, Complete, and Verifiable example on stackblitz.com? I realize the code won't work but it will convey the intention.

– Igor
Mar 7 at 18:32












1 Answer
1






active

oldest

votes


















1














You can't hold extra information in the property like that. A property when assigned will be replaced by the new value, nothing of what you assign not the prototype of the class (and target is the prototype not an instance of the class) will be accessible once you assign a new value.



A solution would be to add an extra property, lets call it meta which could hold this metadata (and other property metadata as needed). You can even type it correctly using mapped types. This solution is pretty close to your desired outcome:



class Model 
readonly meta!: Metadata<Model>
@Required
name: string;

hobbies: string;

constructor(dto?: any)
this.name = dto.name; // This clears my "required" property
this.hobbies = dto.hobbies;


type Metadata<T> = Partial<Record<keyof T, PropertyMetadata>>
type PropertyMetadata =
required?: boolean

function Required<T extends meta: Metadata<T> , TKey extends keyof T>(target: T, key: TKey) (target.meta = );
let propMeta = meta[key]

console.log(new Model().meta.name!.required);


Note that meta can be undefined if you don't have any metadata. And the metadata for a property could likewise be undefined so in the angular template you should access it using ?: model.meta?.name?.required. You could also do some fancier stuff assigning a Proxy instead of to meta but I went the simple route here.






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%2f55050574%2ftypescript-property-decorators%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    You can't hold extra information in the property like that. A property when assigned will be replaced by the new value, nothing of what you assign not the prototype of the class (and target is the prototype not an instance of the class) will be accessible once you assign a new value.



    A solution would be to add an extra property, lets call it meta which could hold this metadata (and other property metadata as needed). You can even type it correctly using mapped types. This solution is pretty close to your desired outcome:



    class Model 
    readonly meta!: Metadata<Model>
    @Required
    name: string;

    hobbies: string;

    constructor(dto?: any)
    this.name = dto.name; // This clears my "required" property
    this.hobbies = dto.hobbies;


    type Metadata<T> = Partial<Record<keyof T, PropertyMetadata>>
    type PropertyMetadata =
    required?: boolean

    function Required<T extends meta: Metadata<T> , TKey extends keyof T>(target: T, key: TKey) (target.meta = );
    let propMeta = meta[key]

    console.log(new Model().meta.name!.required);


    Note that meta can be undefined if you don't have any metadata. And the metadata for a property could likewise be undefined so in the angular template you should access it using ?: model.meta?.name?.required. You could also do some fancier stuff assigning a Proxy instead of to meta but I went the simple route here.






    share|improve this answer



























      1














      You can't hold extra information in the property like that. A property when assigned will be replaced by the new value, nothing of what you assign not the prototype of the class (and target is the prototype not an instance of the class) will be accessible once you assign a new value.



      A solution would be to add an extra property, lets call it meta which could hold this metadata (and other property metadata as needed). You can even type it correctly using mapped types. This solution is pretty close to your desired outcome:



      class Model 
      readonly meta!: Metadata<Model>
      @Required
      name: string;

      hobbies: string;

      constructor(dto?: any)
      this.name = dto.name; // This clears my "required" property
      this.hobbies = dto.hobbies;


      type Metadata<T> = Partial<Record<keyof T, PropertyMetadata>>
      type PropertyMetadata =
      required?: boolean

      function Required<T extends meta: Metadata<T> , TKey extends keyof T>(target: T, key: TKey) (target.meta = );
      let propMeta = meta[key]

      console.log(new Model().meta.name!.required);


      Note that meta can be undefined if you don't have any metadata. And the metadata for a property could likewise be undefined so in the angular template you should access it using ?: model.meta?.name?.required. You could also do some fancier stuff assigning a Proxy instead of to meta but I went the simple route here.






      share|improve this answer

























        1












        1








        1







        You can't hold extra information in the property like that. A property when assigned will be replaced by the new value, nothing of what you assign not the prototype of the class (and target is the prototype not an instance of the class) will be accessible once you assign a new value.



        A solution would be to add an extra property, lets call it meta which could hold this metadata (and other property metadata as needed). You can even type it correctly using mapped types. This solution is pretty close to your desired outcome:



        class Model 
        readonly meta!: Metadata<Model>
        @Required
        name: string;

        hobbies: string;

        constructor(dto?: any)
        this.name = dto.name; // This clears my "required" property
        this.hobbies = dto.hobbies;


        type Metadata<T> = Partial<Record<keyof T, PropertyMetadata>>
        type PropertyMetadata =
        required?: boolean

        function Required<T extends meta: Metadata<T> , TKey extends keyof T>(target: T, key: TKey) (target.meta = );
        let propMeta = meta[key]

        console.log(new Model().meta.name!.required);


        Note that meta can be undefined if you don't have any metadata. And the metadata for a property could likewise be undefined so in the angular template you should access it using ?: model.meta?.name?.required. You could also do some fancier stuff assigning a Proxy instead of to meta but I went the simple route here.






        share|improve this answer













        You can't hold extra information in the property like that. A property when assigned will be replaced by the new value, nothing of what you assign not the prototype of the class (and target is the prototype not an instance of the class) will be accessible once you assign a new value.



        A solution would be to add an extra property, lets call it meta which could hold this metadata (and other property metadata as needed). You can even type it correctly using mapped types. This solution is pretty close to your desired outcome:



        class Model 
        readonly meta!: Metadata<Model>
        @Required
        name: string;

        hobbies: string;

        constructor(dto?: any)
        this.name = dto.name; // This clears my "required" property
        this.hobbies = dto.hobbies;


        type Metadata<T> = Partial<Record<keyof T, PropertyMetadata>>
        type PropertyMetadata =
        required?: boolean

        function Required<T extends meta: Metadata<T> , TKey extends keyof T>(target: T, key: TKey) (target.meta = );
        let propMeta = meta[key]

        console.log(new Model().meta.name!.required);


        Note that meta can be undefined if you don't have any metadata. And the metadata for a property could likewise be undefined so in the angular template you should access it using ?: model.meta?.name?.required. You could also do some fancier stuff assigning a Proxy instead of to meta but I went the simple route here.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 7 at 18:48









        Titian Cernicova-DragomirTitian Cernicova-Dragomir

        72.1k35068




        72.1k35068





























            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%2f55050574%2ftypescript-property-decorators%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

            Save data to MySQL database using ExtJS and PHP [closed]2019 Community Moderator ElectionHow can I prevent SQL injection in PHP?Which MySQL data type to use for storing boolean valuesPHP: Delete an element from an arrayHow do I connect to a MySQL Database in Python?Should I use the datetime or timestamp data type in MySQL?How to get a list of MySQL user accountsHow Do You Parse and Process HTML/XML in PHP?Reference — What does this symbol mean in PHP?How does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?

            Compiling GNU Global with universal-ctags support Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Tags for Emacs: Relationship between etags, ebrowse, cscope, GNU Global and exuberant ctagsVim and Ctags tips and trickscscope or ctags why choose one over the other?scons and ctagsctags cannot open option file “.ctags”Adding tag scopes in universal-ctagsShould I use Universal-ctags?Universal ctags on WindowsHow do I install GNU Global with universal ctags support using Homebrew?Universal ctags with emacsHow to highlight ctags generated by Universal Ctags in Vim?

            Add ONERROR event to image from jsp tldHow to add an image to a JPanel?Saving image from PHP URLHTML img scalingCheck if an image is loaded (no errors) with jQueryHow to force an <img> to take up width, even if the image is not loadedHow do I populate hidden form field with a value set in Spring ControllerStyling Raw elements Generated from JSP tagds with Jquery MobileLimit resizing of images with explicitly set width and height attributeserror TLD use in a jsp fileJsp tld files cannot be resolved