Fp-ts: `unzip` type error with point-free style

Created on 20 Feb 2020  路  11Comments  路  Source: gcanti/fp-ts

馃悰 Bug report

unzip typings seem to behave inconsistently depending on whether point-free or with-argument style is used.

Reproducible example

import { pipe } from 'fp-ts/es6/pipeable'
import * as L from 'fp-ts/es/6/Array';

const f = (xs: Array<number>): [Array<number>, Array<number>] =>
    pipe(
        xs,
        L.map(n => [n + 1, n] as [number, number]),
        L.unzip,
    );

produces:

Type '[unknown[], unknown[]]' is not assignable to type '[number[], number[]]'.
  Type 'unknown[]' is not assignable to type 'number[]'.
    Type 'unknown' is not assignable to type 'number'.ts(2322)

However, if you replace the last line with pairs => L.unzip(pairs) it works perfectly fine in terms of types.

Suggested solution(s)

I suspect it's due to multiple overloads of unzip (introduced in 2.5). I guess if no other workaround is possible we should make unzip woking on NonEmptyArray a separate function in appropriate module?

| Software | Version(s) |
| ---------- | ---------- |
| fp-ts | 2.5.0 |
| TypeScript | 3.7.3 |

regression

Most helpful comment

// const result: [unknown[], unknown[]] // <= regression, was [number[], number[]]
export const result = pipe([[1, 2]], A.unzip)

@vicrac This is a regression, I'll revert https://github.com/gcanti/fp-ts/pull/1120 and add specialized zip, zipWith and unzip to NonEmptyArray

All 11 comments

@vicrac mmhh I can't repro, the reproducible example doesn't raise any error in my local project

Hmm, that's weird. I use Typescript 3.7.3, maybe that's the problem. I'll try to open a PR with reproduction in tests.

Happens for me also,
[email protected]
[email protected]

@vicrac never mind forgot to update fp-ts, sorry

@gcanti no problem :smile: Note: that's definitely unzip overload problem, since removing second overload fixes it.

// const result: [unknown[], unknown[]] // <= regression, was [number[], number[]]
export const result = pipe([[1, 2]], A.unzip)

@vicrac This is a regression, I'll revert https://github.com/gcanti/fp-ts/pull/1120 and add specialized zip, zipWith and unzip to NonEmptyArray

@gcanti Sorry to bother you, but could you explain why did this failed, given these overloads seem to be reasonable? I've spent half an hour trying to figure it out and ended up with literally no idea...

@vicrac honestly? no idea. Keep in mind that there are many places in the fp-ts codebase where type inference doesn't "just work" and I need either an eta expansion (like the pairs => L.unzip(pairs) above) or an explicit type annotation.

I thought that overloadings were "safe"... lesson learned (the hard way).

@vicrac @GrzegorzKazana patch released, could you please try it out?

Just checked, works as intended, both for Array.unzip and NonEmptyArray.unzip. Thanks a lot for quick response 馃

Checked too, works just fine :ok_hand: Thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

miguelferraro picture miguelferraro  路  3Comments

mmkal picture mmkal  路  3Comments

steida picture steida  路  4Comments

bobaaaaa picture bobaaaaa  路  4Comments

Crashthatch picture Crashthatch  路  4Comments