Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

112
Views
Decorators and Private fields javascript

I find myself trying to use decorators with the native javascript private properties (#) and these first 'recognize' that are in use do not work.

I identified this by using the class-validator decorators on the private properties of my value objects.

The error I get in my code editor is: Decorators are not valid here

Example:

import { IsString } from 'class-validator';

Class Person {
  @IsString()
  #name: string;

  constructor(name: string) {
    this.#name = name;
  }

  get name(): string {
    return this.#name;
  }
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Okey as suggested by VLAZ:

Private fields in JS are completely private and inaccessible to anything from outside. Thus it makes sense they cannot be decorated - there is no way for the decorator to access them.

This is completely correct, so when I took a closer look at the value object I realized that it does have public get properties, so by testing it is possible to use decorators on those properties.

Leaving something like:

import { IsString } from 'class-validator';

Class Person {
  #name: string;

  constructor(name: string) {
    this.#name = name;
  }

  @IsString()
  get name(): string {
    return this.#name;
  }
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!