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 all commits
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/Iso.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ Added in v2.3.8
**Signature**

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

Added in v2.3.0
Expand Down
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 = A>(f: (a: A) => B) => <S>(sa: Lens<S, A>) => (s: S) => S
```

Added in v2.3.0
Expand Down
6 changes: 4 additions & 2 deletions docs/modules/Optional.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ Added in v2.3.0
**Signature**

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

Added in v2.3.0
Expand Down Expand Up @@ -252,7 +252,9 @@ Added in v2.3.5
**Signature**

```ts
export declare const modifyOption: <A>(f: (a: A) => A) => <S>(optional: Optional<S, A>) => (s: S) => O.Option<S>
export declare const modifyOption: <A, B extends A = A>(
f: (a: A) => B
) => <S>(optional: Optional<S, A>) => (s: S) => O.Option<S>
```

Added in v2.3.0
Expand Down
4 changes: 2 additions & 2 deletions docs/modules/Prism.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ Added in v2.3.0
**Signature**

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

Added in v2.3.0
Expand Down Expand Up @@ -249,7 +249,7 @@ Added in v2.3.5
**Signature**

```ts
export declare const modifyOption: <A>(f: (a: A) => A) => <S>(sa: Prism<S, A>) => (s: S) => O.Option<S>
export declare const modifyOption: <A, B extends A = A>(f: (a: A) => B) => <S>(sa: Prism<S, A>) => (s: S) => O.Option<S>
```

Added in v2.3.0
Expand Down
2 changes: 1 addition & 1 deletion docs/modules/Traversal.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ Added in v2.3.0
**Signature**

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

Added in v2.3.0
Expand Down
25 changes: 25 additions & 0 deletions dtslint/ts3.5/Iso.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import * as _ from '../../src/Iso'
import { pipe } from 'fp-ts/lib/pipeable'

interface A {
a: string
b: number
c: string | boolean
}

declare const isoC: _.Iso<A, A['c']>

//
// modify
//

// $ExpectType (s: A) => A
pipe(isoC, _.modify((
a // $ExpectType string | boolean
) => a))

// $ExpectType (s: A) => A
pipe(isoC, _.modify<string | boolean>(() => 'foo'))

// $ExpectType (s: A) => A
pipe(isoC, _.modify(() => 'foo'))
19 changes: 18 additions & 1 deletion dtslint/ts3.5/Lens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,26 @@ import { pipe } from 'fp-ts/lib/pipeable'
interface A {
a: string
b: number
c: boolean
c: string | boolean
}

declare const lensC: L.Lens<A, A['c']>

//
// modify
//

// $ExpectType (s: A) => A
pipe(lensC, L.modify((
a // $ExpectType string | boolean
) => a))

// $ExpectType (s: A) => A
pipe(lensC, L.modify<string | boolean>(() => 'foo'))

// $ExpectType (s: A) => A
pipe(lensC, L.modify(() => 'foo'))

//
// prop
//
Expand Down
34 changes: 33 additions & 1 deletion dtslint/ts3.5/Optional.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,41 @@ import { pipe } from 'fp-ts/lib/pipeable'
interface A {
a: string
b: number
c: boolean
c: string | boolean
}

declare const optionalC: O.Optional<A, A['c']>

//
// modifyOption
//

// $ExpectType (s: A) => Option<A>
pipe(optionalC, O.modifyOption((
a // $ExpectType string | boolean
) => a))

// $ExpectType (s: A) => Option<A>
pipe(optionalC, O.modifyOption<string | boolean>(() => 'foo'))

// $ExpectType (s: A) => Option<A>
pipe(optionalC, O.modifyOption(() => 'foo'))

//
// modify
//

// $ExpectType (s: A) => A
pipe(optionalC, O.modify((
a // $ExpectType string | boolean
) => a))

// $ExpectType (s: A) => A
pipe(optionalC, O.modify<string | boolean>(() => 'foo'))

// $ExpectType (s: A) => A
pipe(optionalC, O.modify(() => 'foo'))

// $ExpectError
pipe(O.id<A>(), O.props())
// $ExpectError
Expand Down
38 changes: 37 additions & 1 deletion dtslint/ts3.5/Prism.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,45 @@ import { pipe } from 'fp-ts/lib/pipeable'
interface A {
a: string
b: number
c: boolean
c: string | boolean
}

declare const prismC: P.Prism<A, A['c']>

//
// modifyOption
//

// $ExpectType (s: A) => Option<A>
pipe(prismC, P.modifyOption((
a // $ExpectType string | boolean
) => a))

// $ExpectType (s: A) => Option<A>
pipe(prismC, P.modifyOption<string | boolean>(() => 'foo'))

// $ExpectType (s: A) => Option<A>
pipe(prismC, P.modifyOption(() => 'foo'))

//
// modify
//

// $ExpectType (s: A) => A
pipe(prismC, P.modify((
a // $ExpectType string | boolean
) => a))

// $ExpectType (s: A) => A
pipe(prismC, P.modify<string | boolean>(() => 'foo'))

// $ExpectType (s: A) => A
pipe(prismC, P.modify(() => 'foo'))

//
// props
//

// $ExpectError
pipe(P.id<A>(), P.props())
// $ExpectError
Expand Down
19 changes: 18 additions & 1 deletion dtslint/ts3.5/Traversal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,26 @@ import { pipe } from 'fp-ts/lib/pipeable'
interface A {
a: string
b: number
c: boolean
c: string | boolean
}

declare const traversalC: T.Traversal<A, A['c']>

//
// modify
//

// $ExpectType (s: A) => A
pipe(traversalC, T.modify((
a // $ExpectType string | boolean
) => a))

// $ExpectType (s: A) => A
pipe(traversalC, T.modify<string | boolean>(() => 'foo'))

// $ExpectType (s: A) => A
pipe(traversalC, T.modify(() => 'foo'))

// $ExpectError
pipe(T.id<A>(), T.props())
// $ExpectError
Expand Down
3 changes: 2 additions & 1 deletion src/Iso.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@ export const reverse = <S, A>(sa: Iso<S, A>): Iso<A, S> => iso(sa.reverseGet, sa
* @category combinators
* @since 2.3.0
*/
export const modify = <A>(f: (a: A) => A) => <S>(sa: Iso<S, A>) => (s: S): S => sa.reverseGet(f(sa.get(s)))
export const modify = <A, B extends A = A>(f: (a: A) => B) => <S>(sa: Iso<S, A>) => (s: S): S =>
sa.reverseGet(f(sa.get(s)))

/**
* @category combinators
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 = 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
8 changes: 5 additions & 3 deletions src/Optional.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,9 @@ export const composeTraversal = <A, B>(ab: Traversal<A, B>): (<S>(sa: Optional<S
* @category combinators
* @since 2.3.0
*/
export const modifyOption: <A>(f: (a: A) => A) => <S>(optional: Optional<S, A>) => (s: S) => Option<S> =
_.optionalModifyOption
export const modifyOption: <A, B extends A = A>(
f: (a: A) => B
) => <S>(optional: Optional<S, A>) => (s: S) => Option<S> = _.optionalModifyOption

/**
* @category combinators
Expand All @@ -162,7 +163,8 @@ export const setOption = <A>(a: A): (<S>(optional: Optional<S, A>) => (s: S) =>
* @category combinators
* @since 2.3.0
*/
export const modify: <A>(f: (a: A) => A) => <S>(optional: Optional<S, A>) => (s: S) => S = _.optionalModify
export const modify: <A, B extends A = A>(f: (a: A) => B) => <S>(optional: Optional<S, A>) => (s: S) => S =
_.optionalModify

/**
* @category combinators
Expand Down
5 changes: 3 additions & 2 deletions src/Prism.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,14 @@ export const set: <A>(a: A) => <S>(sa: Prism<S, A>) => (s: S) => S = _.prismSet
* @category combinators
* @since 2.3.0
*/
export const modifyOption: <A>(f: (a: A) => A) => <S>(sa: Prism<S, A>) => (s: S) => Option<S> = _.prismModifyOption
export const modifyOption: <A, B extends A = A>(f: (a: A) => B) => <S>(sa: Prism<S, A>) => (s: S) => Option<S> =
_.prismModifyOption

/**
* @category combinators
* @since 2.3.0
*/
export const modify: <A>(f: (a: A) => A) => <S>(sa: Prism<S, A>) => (s: S) => S = _.prismModify
export const modify: <A, B extends A = A>(f: (a: A) => B) => <S>(sa: Prism<S, A>) => (s: S) => S = _.prismModify

/**
* @category combinators
Expand Down
2 changes: 1 addition & 1 deletion src/Traversal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ export const composeOptional: <A, B>(ab: Optional<A, B>) => <S>(sa: Traversal<S,
* @category combinators
* @since 2.3.0
*/
export const modify = <A>(f: (a: A) => A) => <S>(sa: Traversal<S, A>): ((s: S) => S) =>
export const modify = <A, B extends A = A>(f: (a: A) => B) => <S>(sa: Traversal<S, A>): ((s: S) => S) =>
sa.modifyF(_.ApplicativeIdentity)(f)

/**
Expand Down
10 changes: 6 additions & 4 deletions src/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ export const prismAsTraversal = <S, A>(sa: Prism<S, A>): Traversal<S, A> =>
)

/** @internal */
export const prismModifyOption = <A>(f: (a: A) => A) => <S>(sa: Prism<S, A>) => (s: S): O.Option<S> =>
export const prismModifyOption = <A, B extends A>(f: (a: A) => B) => <S>(sa: Prism<S, A>) => (s: S): O.Option<S> =>
pipe(
sa.getOption(s),
O.map((o) => {
Expand All @@ -179,7 +179,7 @@ export const prismModifyOption = <A>(f: (a: A) => A) => <S>(sa: Prism<S, A>) =>
)

/** @internal */
export const prismModify = <A>(f: (a: A) => A) => <S>(sa: Prism<S, A>): ((s: S) => S) => {
export const prismModify = <A, B extends A>(f: (a: A) => B) => <S>(sa: Prism<S, A>): ((s: S) => S) => {
const g = prismModifyOption(f)(sa)
return (s) =>
pipe(
Expand Down Expand Up @@ -238,7 +238,9 @@ export const optionalAsTraversal = <S, A>(sa: Optional<S, A>): Traversal<S, A> =
)

/** @internal */
export const optionalModifyOption = <A>(f: (a: A) => A) => <S>(optional: Optional<S, A>) => (s: S): O.Option<S> =>
export const optionalModifyOption = <A, B extends A>(f: (a: A) => B) => <S>(optional: Optional<S, A>) => (
s: S
): O.Option<S> =>
pipe(
optional.getOption(s),
O.map((a) => {
Expand All @@ -248,7 +250,7 @@ export const optionalModifyOption = <A>(f: (a: A) => A) => <S>(optional: Optiona
)

/** @internal */
export const optionalModify = <A>(f: (a: A) => A) => <S>(optional: Optional<S, A>): ((s: S) => S) => {
export const optionalModify = <A, B extends A>(f: (a: A) => B) => <S>(optional: Optional<S, A>): ((s: S) => S) => {
const g = optionalModifyOption(f)(optional)
return (s) =>
pipe(
Expand Down