Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix modify type inference #181

Merged
merged 3 commits into from
Apr 5, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/modules/Lens.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ Added in v2.3.0
**Signature**

```ts
export declare const modify: <A>(f: (a: A) => A) => <S>(sa: Lens<S, A>) => (s: S) => S
export declare const modify: <A, B extends A>(f: (a: A) => B) => <S>(sa: Lens<S, A>) => (s: S) => S
```

Added in v2.3.0
Expand Down
14 changes: 13 additions & 1 deletion dtslint/ts3.5/Lens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,21 @@ import { pipe } from 'fp-ts/lib/pipeable'
interface A {
a: string
b: number
c: boolean
c: string | boolean
}

//
// modify
//

// $ExpectType (s: A) => A
pipe(L.id<A>(), L.prop('c'), L.modify((
a // $ExpectType string | boolean
) => a))

// $ExpectType (s: A) => A
pipe(L.id<A>(), L.prop('c'), L.modify(() => 'foo'))

//
// prop
//
Expand Down
2 changes: 1 addition & 1 deletion src/Lens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export const composeTraversal = <A, B>(ab: Traversal<A, B>): (<S>(sa: Lens<S, A>
* @category combinators
* @since 2.3.0
*/
export const modify = <A>(f: (a: A) => A) => <S>(sa: Lens<S, A>) => (s: S): S => {
export const modify = <A, B extends A>(f: (a: A) => B) => <S>(sa: Lens<S, A>) => (s: S): S => {
const o = sa.get(s)
const n = f(o)
return o === n ? s : sa.set(n)(s)
Expand Down