Is there a way to create an object with keys that are a subset of the keys of a generic type?
I am exploring options for defining a type for the headers parameter, which should be a subset of the keys of 'T' mapping to string values.
export abstract class Table<T> {
constructor(
protected data: T[],
//this currently requires the headers object to include all keys in T
protected headers: { [key in keyof T]: string },
//however, I am looking for a solution like this
protected headers: { [keyof T]: string }
) {}
//...abstract methods
}
//example
interface User {
username: string;
password: string;
age: number;
}
class UserTable extends Table<User> {
constructor(data: User[]) {
//the current implementation does not compile as it lacks all keys from User
super(data, {
username: 'User',
age: 'Age',
});
}
}