Is it possible in TypeScript to automatically create a class with properties based on its generic type? For example:
class CustomClass<T> {
// Code to extract properties from T should go here
}
interface Characteristics {
quality: string;
price: number;
}
const item = new CustomClass<Characteristics>;
// Now, both item.quality and item.price will be available for autocompletion with the correct types specified in the 'Characteristics' interface
interface Attributes {
color: string;
size: number;
weight: number;
}
const product = new CustomClass<Attributes>;
// Only the three attributes defined in 'Attributes' interface will be accessible for 'product'
I am looking for a solution that does not involve adding something like
class SomeClass {
[property: string]: any
}
as this would allow any property to be added, whereas I specifically want to limit the properties to those defined in the generic type.