Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

243
Views
How to constrain a generic type in Flow to allow compare operators like < or lessThen?

Consider the following generic function in Flow:

function lessThen<T>(a: T, b: T) {
  return a < b;
}

The intention here is to instantiate lessThen with a type that allows the < operator like number or string.

In this form Flow complains about it:

return a < b;
            ^ Cannot compare `T` [1] to `T` [2]. [invalid-compare]
References:
1: function lessThen<T>(a: T, b: T) {
                           ^ [1]
1: function lessThen<T>(a: T, b: T) {
                                 ^ [2]

So I suppose I need to constrain T in lessThen to support <. But how do I do that? I was looking to something like build-in type CanUseCompareOperators so I can write:

function lessThen<T: CanUseCompareOperators>(a: T, b: T) {
  return a < b;
}

but I cannot find any.

Although in this toy example I can drop the generics and let Flow infere the type, in the real code I need this to work for a methods in a generic class when dropping generics is not an option like in:

class Foo<Key, Value> {
  binarySearch() {
    // code that uses < and > to compare keys
  }
}

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!