facebook / facebook/flow

Support for Comparables with `valueOf`

Đang mở
#2,406 1 bình luận 4 reaction 0 người được giao Xem trên GitHub
feature request
Ngôn ngữ chính
Rust
Star
22.3k
Fork
1.9k
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

Consider this:

``` javascript
interface Comparable {
valueOf(): number;
}

class Comparer {

greater (first: Key, second: Key): boolean {
// flow error: Cannot be compared to Comparable
return first > second;
}
}
```

The [`valueOf()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/valueOf) method gets special treatment in Javascript and returns the primitive value for an Object type.

It can be implemented by any object so that they can be used in comparisons with the '<', '==', '===' and '>' operators. However, from the example above it appears to be the case that flow doesn't understand that the `valueOf()` method in the `Comparable` interface can be used without calling `first.valueOf() > second.valueOf()` directly.

I'd love to see this implemented in flow. It will be nice if flow throws the error only if the given item is an object type and doesn't implement `valueOf()`.

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.