Looking for some guidance on working with rest parameters in a constructor. Specifically, I have a scenario where the rest parameter should consist of keys from an object, and I want to ensure that when calling the constructor, only unique keys are passed.
I'm not sure if this is even possible, but if it is, I would greatly appreciate any help or insights on how to achieve it.
Below is a snippet of code to illustrate my issue:
class MyClass<GenType> {
// Using the Utility Type Partial<?>, I managed to restrict the keys to those existing in GenType,
// but it still allows duplicate key entries
constructor(...rest: Array<Partial<keyof GenType>>) {
// constructor implementation
}
}
type MyGenericType = {
key1: string,
key2: number,
key3: Array<number>
}
// Currently, the constructor permits duplicate keys like "key1", "key2", "key1"
const myClassObject = new MyClass<MyGenericType>("key1", "key2", "key1");
Here's an example of the problem I'm facing in my actual code:
Currently, it accepts duplicate keys and suggests all object keys rather than just the remaining ones.
Is there a way to achieve the desired effect? Any suggestions or solutions would be highly appreciated.
Thank you! 😊