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
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
add a comment |
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
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
add a comment |
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
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
angular typescript validation decorator
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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.
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
answered Mar 7 at 18:48
Titian Cernicova-DragomirTitian Cernicova-Dragomir
72.1k35068
72.1k35068
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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