fp-ts icon indicating copy to clipboard operation
fp-ts copied to clipboard

Bind and BindW improperly passes params through

Open feydan opened this issue 4 years ago • 7 comments

🐛 Bug report

The following code improperly raises a type error:

  const func1 = (i: {arg1: string}) => TE.right('result1')
  const func2 = (i: {arg2: string, result1: string}) => TE.right('result2')

  pipe(
    TE.right({arg1: 'arg1', arg2: 'arg2'}),
    TE.bindW('result1', func1),
    TE.bindW('result2', func2),
  )

This is because this line improperly rewrites the parameter list that gets passed into the next function in the pipe TE.bindW('result1', func1),

Current Behavior

Improper type error

Expected behavior

The params should be passed through using the return type of the previous pipe call rather than the param list of the function passed in.

Reproducible example

Example above

Suggested solution(s)

I'll open an MR to fix this following this bug issue.

Additional context

Your environment

fp-ts 2.11.2

Software Version(s)
fp-ts 2.11.2
TypeScript 4.2.3

feydan avatar Sep 15 '21 18:09 feydan

+1

I keep hitting the same problem. This has been open for quite some time, with a PR to fix it.. What is preventing the fix to be released?

Thx

jrmdayn avatar Mar 24 '22 12:03 jrmdayn

Reopening because the fix causes a regression (https://github.com/gcanti/fp-ts/pull/1584#issuecomment-1252053927)

gcanti avatar Sep 20 '22 10:09 gcanti

@gcanti The ts-pattern problem is confirmed as a limitation in TypeScript (https://github.com/gvergnaud/ts-pattern/issues/111#issuecomment-1261961683). I have a workaround for when the change is made, and it's definitely one to do. (Just not sure whether it should be left for v3.)

thewilkybarkid avatar Oct 03 '22 08:10 thewilkybarkid

The release notes for v2.13.1 suggest that this was fixed in that release but I am still seeing this behaviour after updating and the comments here suggest the fix was removed from the release?

Could you confirm that it is the release notes that are incorrect and I am not going mad lol?

georgegebbett avatar Oct 19 '22 10:10 georgegebbett

@georgegebbett sorry, I forgot to amend the changelog after reverting the change (the fix was actualy removed from the release)

gcanti avatar Oct 19 '22 11:10 gcanti

That makes what I am seeing make sense! Thanks for clarifying!

georgegebbett avatar Oct 19 '22 11:10 georgegebbett

With TypeScript 5.4, f: (a: NoInfer<A>) => TaskEither<E2, B> might solve the problem.

For example

export const bindW: <N extends string, A, E2, B>(
  name: Exclude<N, keyof A>,
  f: (a: NoInfer<A>) => TaskEither<E2, B>
) => <E1>(fa: TaskEither<E1, A>) => TaskEither<E1 | E2, { readonly [K in keyof A | N]: K extends keyof A ? A[K] : B }> =
  bind as any

However, I have no idea how difficult it is to bump TypeScript and find possible regressions because currently, type errors in tests are not being detected.

chao7150 avatar Apr 08 '24 12:04 chao7150