Currently, I am in the process of creating a validator class. Here's what it looks like:
class Validator {
private path: string;
private data: unknown;
constructor(path: string, data: string) {
this.data = data;
this.path = path;
}
public isString() { /* ... */ }
}
At the moment, the type of my data property is unknown
. However, I would like for its type to be inherited from the constructor parameter, like so:
const validator = new Validator("HomePage", 123); // data in class should be inherited as number
When working with functions, I usually use generics to achieve this, such as:
function<T>(path: string, data: T) { }
But I am encountering difficulty figuring out how to implement this functionality with classes, specifically inheriting from the constructor.