Currently facing an issue with Typescript where I'm attempting to ensure that any objects extending an abstract class must define properties from a passed-in interface as a generic. Let me illustrate this with an example:
interface RelationshipData {
data: {
id: string;
type: string;
}
}
interface Dto<A = Record<string, any>, R = Record<string, RelationshipData>> {
attributes: A;
relationships: R;
}
abstract class EntityModel<T extends Dto> {
// How do I make sure that T['attributes']'s properties and values are defined in this class?
// The following code snippet produces errors
[key in T['attributes']: T['attributes'][key];
}
Let's dive into how the implementation would look like:
interface ProductAttributes {
name: string;
description: string;
}
interface ProductRelationships {
tenant: RelationshipData;
}
interface ProductDto extends Dto<ProductAttributes, ProductRelationships> {}
export class ProductModel extends EntityModel<ProductDto> {
/**
* My intention is for Typescript to flag an error here if I haven't defined the following:
*
* name: string;
* description: string;
*/
}
I've experimented with the above approach but unfortunately, it doesn't seem to work.