// RepositoryBase.ts
export type FieldsMapping<T> = { readonly [k in keyof Required<T>]: string };
export abstract class RepositoryBase<T> {
protected abstract readonly COLUMNS: FieldsMapping<T>;
}
// UsersRepository.ts
import { FieldsMapping, RepositoryBase } from './RepositoryBase';
interface User {
firstName: string;
lastName: string;
email?: string;
}
export class UsersRepository extends RepositoryBase<User> {
protected readonly COLUMNS = {
firstName: 'first_name',
lastName: 'last_name',
email: 'email',
extra: 'non_existing_column',
};
}
The declaration of COLUMNS in UsersRepository doesn't give any compiling error, even tho extra key doesn't exist on User interface.
If I add the type to COLUMNS in UserRepository, such as: COLUMNS: FieldsMapping<User>, then the error is thrown.
I don't want to redeclare the type on each class which inherits RepositoryBase. Do you have any idea why this is happening? Is maybe due to the abstract? Or maybe is it due to some configuration in tsconfig.json? Any idea how to solve it?