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

192
Views
Cómo obtener el tipo genérico de clase en TypeScript

Necesito intentar pasar el tipo genérico a una clase en TypeScript.

Vea mi ejemplo a continuación, inicializo mi clase con el tipo 'hello' | 'goodbye'

Luego quiero crear una función con el parámetro del tipo pasado a la clase original.

es posible?

 class MyTestClass<T> { doSomething = (myType: T, count: number) => { // ... } } const myTest = new MyTestClass<'hello' | 'goodbye'>() const myNewMethod = (subType: typeof myTest) => { // I want subType to be 'hello' | 'goodbye' }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Puede usar tipos condicionales con la palabra clave infer para extraer la U de MyTestClass<U>

 type subType = typeof mytest extends MyTestClass<infer U> ? U : never;

Aquí, si myTest es algún tipo de MyTestClass<X> , entonces subType será X , de lo contrario, never lo será (lo que nunca sucederá ya que myTest es explícitamente MyTestClass .

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!