mobily / ts-belt

🔧 Fast, modern, and practical utility library for FP in TypeScript.
https://mobily.github.io/ts-belt
MIT License
1.08k stars 30 forks source link

ts-belt usage example flagged by TypeScript #89

Closed zprayz closed 7 months ago

zprayz commented 10 months ago

In the the first ts-belt usage example the line: O.map(N.multiply(10)), is flagged as being a problem:

Argument of type '(option: Option<number>) => Option<number>' is not assignable to parameter of type '(arg: Option<number>) => Option<0>'.
  Type 'Option<number>' is not assignable to type 'Option<0>'.
    Type 'number' is not assignable to type 'Option<0>'.
import { A, O, N, pipe } from "@mobily/ts-belt";

pipe(
  [1, 2, 3, 4, 5], // → [1, 2, 3, 4, 5]
  A.dropExactly(2), // → Some([3, 4, 5])
  O.flatMap(A.head), // → Some(3)
  O.map(N.multiply(10)), // → Some(30)  **************** this line flagged
  O.getWithDefault(0) // → 30
); // → 30

The code compiles and runs fine

If I translate the code to ReScript there are no issues:

let result = {
  open Belt
  let dropExactly = (xs, n) =>
    n < 0 || n > Array.length(xs) ? None : Some(Array.sliceToEnd(xs, n))
  let head = xs => xs[0]
  let multiply = (a, b) => a * b

  [1, 2, 3, 4, 5] // → [1, 2, 3, 4, 5]
  ->dropExactly(2) // → Some([3, 4, 5])
  ->Option.flatMap(head) // → Some(3)
  ->Option.map(multiply(10)) // → Some(30)  *************** no issue in ReScript
  ->Option.getWithDefault(0) // → 30
  // → 30
}

So is this simply a TypeScript problem/limitation?

JUSTIVE commented 7 months ago

this issue seems resolved in ts-belt v4.

zprayz commented 7 months ago

Indeed it seems to be the case.