I have a problem I’m trying to solve to get Typescript to ensure that any objects that extend an abstract class, define properties from an interface that’s passed in as a generic. Let me try to give you 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> extends BaseEntity<T> {
// How can I get T['attributes']'s properties and values to be defined on this class?
// Doing this just errors
[key in T['attributes']: T['attributes'][key];
[key in T['relationships']: Type<EntityModel>;
}
Here’s how the actual implementation would work:
interface ProductDto {
attributes: {
name: string;
description: string;
},
relationships: {
tenant: {
data: RelationshipData;
}
}
}
export class ProductModel extends EntityModel<ProductDto> {
/**
* I want Typescript to error here that I haven't defined the following:
*
* name: string;
* description: string;
* tenant: TenantModel;
*/
}
I’ve tried the above but it doesn’t work.