type MatchOperator = "==" | ">" | "<";
type Criteria<T, P extends keyof T> = {
field: P,
value: T[P],
operator: MatchOperator,
}
interface User {
name: string;
age: number;
id: number;
}
const adultCriteria: Criteria<User, "age"> = {
field: "age",
operator: ">",
value: 18
}
Is there a better way to restrict the type of value
based on field
using Typescript as mentioned below?
const adultCriteria: Criteria<User> = {
field: "age",
operator: ">",
value: 18
}