// BaseRepository.ts
export type FieldMappings<T> = { readonly [k in keyof Required<T>]: string };
export abstract class BaseRepository<T> {
protected abstract readonly FIELDS: FieldMappings<T>;
}
// ProductsRepository.ts
import { FieldMappings, BaseRepository } from './BaseRepository';
interface Product {
name: string;
price: number;
category?: string;
}
export class ProductsRepository extends BaseRepository<Product> {
protected readonly FIELDS = {
name: 'product_name',
price: 'product_price',
category: 'product_category',
description: 'description', // extra field
};
}
There is no compile error when adding an additional description
field to the FIELDS
declaration in the ProductsRepository
, even though it's not part of the Product
interface.
Addition of a type declaration to the FIELDS
property in the ProductsRepository
, like
FIELDS: FieldMappings<Product>
, will result in a compilation error.
Avoiding redeclaration of the type for every class inheriting from BaseRepository
is desired. Is this behavior possibly linked to the use of abstract
? Or perhaps it relates to some settings in the tsconfig.json
file? Any insights on how to rectify this issue would be appreciated.