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

399
Vistas
Is it possible to define a set of rest arguments in TypeScript?

I don't know if the question title was sugestive but my problem is, in a constructor I got a rest parameter that must be the keys of an object and I want that, when I call the constructor, it only allows the keys that isn't yet used.

I don't know if it's possible, but if it is, I would be glad if someone can help me whit it.

Some code to ilustrate my problem below:


class MyClass<GenType> {
    // With the Utility Type Partial<?> I achieved the behavior of allowing only the keys
    // that the object type GenType has, but it still allows that the same key can be passed
    // as argument more than once
    constructor(...rest: Array<Partial<keyof GenType>>) {

    // constructor stuff
    }

}

type MyGenericType = {
    key1: string,
    key2: number,
    key3: Array<number>
}

// Here, in the initialization, it allows to insert the same key multiple times, but
// i want it to allow that a key can be passed just once
const myClassObject = new MyClass<MyGenericType>("key1", "key2", "key1");

Bellow an image of the problem in my real code:

real code problem

Note that it allow to pass the same key more than once and the code sugestion show all the object keys, I want that it didn't allow the same key more than once and the code sugestion only shows the remaining keys

The effect that I want is possible? If yes, how could I achieve it?

Thanks! 😊

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

0

For everyone that is seeking for something similar, with the comments posted I assume that yes, it's possible to achieve it, but no, there is not easy or eficient way to do that, some of the possibilities are in the links bellow:

Possibility 1

Possibility 2

Thanks for all the comments trying to help and hopes that it helps others! 😃

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