← Commure Interview Insights

Commure·Frontend Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
Apr 2026Remote

Summary

Got a TypeScript coding question for a Frontend Engineer role at Commure. Pretty focused on type safety and generics, which I wasn't fully warmed up for.

Questions Asked (1)

Q1

In TypeScript, write a function that takes a list of Sim objects and a condition object, then returns only the Sims that satisfy that condition. Each Sim has fields like age, occult type, and salary. The condition represents a single comparison on one field. How would you type this in a clean, type-safe way?

Technical Trade-offsAlgorithms & Data Structures
Author's notes

My first instinct was to just pass a callback predicate and call it a day, but I could tell they wanted something more structured.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by defining a discriminated union for the condition object, where each variant specifies a field and a comparison operator, ensuring type safety. Then, implement a generic filter function that uses a type guard to narrow the Sim type based on the condition, and apply the appropriate comparison logic. Finally, discuss trade-offs between simplicity and extensibility, and mention potential performance considerations.

Pro tip: Demonstrate awareness of TypeScript's type system by using a mapped type to derive the condition type from the Sim type, ensuring that any changes to Sim automatically update the condition type. Also, mention that for production code, you'd consider using a library like zod for runtime validation if conditions come from external sources.

1. Define the Sim type and condition type

Clearly define the Sim interface with fields like age, occultType, and salary. Then, define a discriminated union for the condition, where each variant includes a field, an operator (e.g., 'eq', 'gt', 'lt'), and a value of the appropriate type.

2. Implement the filter function with type safety

Write a generic function that takes an array of Sim and a condition, and returns a filtered array. Use a switch on the condition's field or operator to apply the correct comparison, ensuring type safety by leveraging TypeScript's narrowing.

3. Handle type narrowing and generics

If needed, use a type guard to narrow the Sim type based on the condition, especially if the condition can be on a field that might have different types. Consider making the function generic over the field to maintain type safety.

4. Discuss trade-offs and extensibility

Talk about the trade-offs between a simple switch-based approach and a more extensible one using a map of operators. Mention how to extend the condition type to support multiple conditions or logical operators.

5. Consider performance and edge cases

Mention that the function should handle empty arrays and invalid conditions gracefully. Discuss performance implications of filtering large arrays and potential optimizations like early termination or indexing.

Key Points to Mention

  • Discriminated unions for type-safe conditions
  • Generic functions and type guards for narrowing
  • Mapped types to derive condition types from Sim
  • Trade-offs between simplicity and extensibility
  • Runtime validation for external conditions
  • Performance considerations for large datasets

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.