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

123
Views
Class syntax with parametrized class name

I want create classes dynamicaly in runtime, and I want parametrize the class name.

function newClass(className:string) {
   return class MyDynamicClass { }
}

// expected class name: "Person"
let personClass = newClass("Person")

// expected class name: "Contact"
let contactClass = newClass("Contact")

P.S.: The proposed duplicate question (ES6 Dynamic class names) is with wrong accepted answer and other answers does not fulfil my requirements.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I don't think it is possible to make dynamic class name like here:

class [className] {}

It is not allowed. However, you can create an object with className property (class expression):

const newClass = (className: string) => ({
  [className]: class { }
}[className])

// expected class name: "Person"
let personClass = newClass("Person")
personClass.name // Person

I know, personClass returns class {} without name. I think this is the closest working solution.

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!