This tripped me up more than it should have.
Start by clarifying the requirements: conditions are field-level comparisons combined with AND logic. Then propose a type-safe representation using a discriminated union of condition objects, each with a field key, operator, and value, and implement a generic filter function that applies all conditions. Emphasize scalability by making it easy to add new fields or operators without breaking existing code.
Pro tip: Mention that you would use TypeScript's mapped types and generics to ensure the value type matches the field type, and that you'd consider performance by short-circuiting conditions and potentially indexing fields if the dataset is large.
Ask about the expected number of conditions, fields, and data size. Confirm that conditions are combined with AND and that each condition targets a single field.
Use a discriminated union where each condition has a field (keyof T), an operator (e.g., 'eq', 'gt'), and a value typed to match the field. Leverage generics to tie the value type to the field.
Write a function that takes an array of T and an array of conditions, and returns T[] where every condition is satisfied. Use Array.prototype.every for clarity.
Design the condition type to be easily extended with new operators or fields. Consider using a map of operator functions to avoid switch statements.
Mention performance considerations like short-circuiting, indexing, or pre-compiling conditions. Also discuss error handling for invalid field/operator combinations.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.