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

400
Views
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 answers
Answer question

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 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!