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

193
Views
Usando la interfaz Typescript como una variable

Estoy trabajando en una arquitectura Node Js (TypeScript) y, por alguna razón, quiero vincular mi interfaz a un objeto específico. Estoy creando una clase general que se amplía con otras subclases y tendrá un código muy general. Así que mi código se ve como

 interface User { name: string; } interface Profile { title: string; } class Parent { name: string; interface: Interface; // Help required here, getting error can't use type as a variable constructor( name, interface ) { // Load schema and store here this.name = name this.interface = interface } // Though this is not correct I hope you get the idea of what I am trying to do get (): this.interface { // fetch the data and return return data } set (data: this.interface): void { // adding new data } } class UserSchema extends Parent { // Class with custom functions for UserSchema } class ProfileSchema extends Parent { // Class with custom functions for ProfileSchema } // Config file that saves the configs for different modules const moduleConfig = [ { name: "User Module", class: UserSchema, interface: User }, { name: "Profile Module", class: ProfileSchema, interface: Profile }, ] const allModules = {} // Loading the modules moduleConfig.map(config => { allModules[config.name] = new config.class( config.name, config.interface ) }) export allModules;

Necesito sugerencias sobre cómo vincular mis interfaces con sus respectivas configuraciones. Hasta ahora no he tenido suerte con eso.

PD: Todo este código se encuentra separado en sus respectivos archivos.

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

0

Este es el caso de uso de los genéricos . Incluso puede verlos como "variable para tipos".

En lugar de tener una propiedad de interface en su Parent , esta última tendría un tipo genérico :

 class Parent<T> { // T is the generic type name: string; // interface: Interface; // generic is already provided at class level constructor( name ) { // Load schema and store here this.name = name } get (): T { // fetch the data and return return data } set (data: T): void { // adding new data } } // Here you specify the concrete generic type class UserSchema extends Parent<User> { // Class with custom functions for UserSchema } class ProfileSchema extends Parent<Profile> { // Class with custom functions for ProfileSchema }
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!