in the example I want to write an simple query builder where all types of an entity are correctly casted. For example when im filtering with equalsTo for name I only want to allow strings as values, same for age there should only be numbers allow. Now value can be any value of Dogs and thats not what I want. Can somebody help me is this possible?
class Dogs {
name!: string;
age!: number;
alive!: boolean;
}
interface QueryBuilder<O extends object, F extends keyof O = keyof O> {
equalsTo: (fieldName: F, value: O[F]) => boolean;
}
const queryBuilder = <O extends object>(): QueryBuilder<O> => ({
equalsTo: (fieldName, value) => true
})
const qb = queryBuilder<Dogs>();
qb.equalsTo("name", 1); // Should throw an error that only string is allowed as value
qb.equalsTo("age", "Rex"); // Should throw an error that only number is allowed as value