mobily / ts-belt

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

Example Code on Array Docs Gives a Warning #34

Closed ahmaddynugroho closed 2 years ago

ahmaddynugroho commented 2 years ago

Link: https://mobily.github.io/ts-belt/api/array node: v18.2.0 typescript: 4.7.3 tsserver: 0.11.1 tsconfig.json: i follow this https://mobily.github.io/ts-belt/docs/getting-started/config

I just tried the code from the array doc and it gives a warning like this: image

the code runs fine, but the warning annoys me. So i removed the warning by handling the Option using match:

import { A, O, pipe } from "@mobily/ts-belt";

const arr = A.makeWithIndex(5, (i) => i); // if i changed this to [] without O.match, it will throws error
console.log(
  pipe(
    arr,
    A.tail,
    O.match( // initially i wanna use O.getWithDefault([]), but it also gives warning. O.getExn works but throws error if arr = [] :(
      (tail: readonly number[]) => tail,
      () => []
    ),
    A.take(2),
    A.map((val) => val * 2)
  )
);

question: is it some secret pipe behavior that handles Option or A.take type is not complete or the doc needs an update?

thank you

ahmaddynugroho commented 2 years ago

i just removed the Option handling and change A.tail to A.tailOrEmpty and the code runs fine and no warning. So, i propose that the docs should use A.tailOrEmpty because it returns Array<A> instead of Option<Array<A>>

mobily commented 2 years ago

@ahmaddynugroho thanks, good catch! 👀 (fixed)