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

150
Views
Cómo pasar una cadena como genérico en TypeScript

Quiero crear un tipo genérico que pueda usar para transformar el tipo normal en un tipo que cumpla con la estructura de respuesta de mi API. Entonces, el problema es que tengo un montón de tipos y quiero inferir de ellos otro montón de tipos que se verán así:

 // this is a type that i have interface IModel { key1: string key2: string } //this is a type I want to infer interface IModelResponse { model: { key1: string key2: string } } // the way i want to do this is through generics in TypeScript // so let's say something like this: type IModelResponse = IBaseResponse<IModel, 'model'>

Intenté algo como esto:

 export interface IBaseResponse<T, modelName extends string> { [key: modelName]: T }

pero me da un error "Un tipo de parámetro de firma de índice debe ser 'cadena' o 'número'".

¿Hay algo que no entiendo o es imposible implementarlo de la manera que pretendo? Agradecería algo de ayuda!

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

0

Se puede implementar usando la palabra clave in

 interface IModel { key1: string key2: string } type BaseResponse<T, ModelName extends string> = { [key in ModelName]: T } type IModelResponse = BaseResponse<IModel, 'model'>
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!