Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

155
Vistas
How to define a class with dynamic keys?

I need to define a class with dynamic props like 'Methods'. I wrote this

type Methods = {
    [name:string]:Function
}

class App <M extends Methods> { 
    [name: keyof M] : M[name] // dont work
}

var methods = {
    action : Function,
    move : Function
}

type M = keyof typeof methods;

type myApp = App<typeof methods>;

but I get the following errors. Can these be fixed?

An index signature parameter type cannot be a literal type or generic type. Consider using a mapped object type instead.

'name' refers to a value, but is being used as a type here. Did you mean 'typeof name'?

Return type of index signature from exported interface has or is using private name 'name'.

Reproducible example

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You seem to be looking for mapped types. Those only work for type declarations, not for classes though:

type App<M extends Methods> = { 
    [name in keyof M]: M[name];
}

(Full demo)

If you actually want to define a class, you need to do it with a concrete type:

var methods = {
    action: Function,
    move: Function
}
type MyApp = App<typeof methods>;

class TheApp implements MyApp {
    action!: typeof Function;
    move!: typeof Function;
    constructor() {
        Object.assign(this, methods);
    }
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda