Skip to content

Commit

Permalink
better filter typings
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti committed Nov 26, 2022
1 parent a3e4342 commit 2209531
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
28 changes: 21 additions & 7 deletions dtslint/ts4.7/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,37 @@ type ST = readonly [string, number]
type TT = readonly [boolean, number]

//
// key
// at
//

// $ExpectType Lens<S, string>
Optic.id<S>().compose(Optic.key('a'))
Optic.id<S>().compose(Optic.at('a'))

// $ExpectType PolyLens<S, T, string, boolean>
Optic.id<S, T>().compose(Optic.key<S, 'a', boolean>('a'))
Optic.id<S, T>().compose(Optic.at<S, 'a', boolean>('a'))

// $ExpectType Lens<ST, string>
Optic.id<ST>().compose(Optic.key('0'))
Optic.id<ST>().compose(Optic.at('0'))

// $ExpectType PolyLens<ST, TT, string, boolean>
Optic.id<ST, TT>().compose(Optic.key<ST, '0', boolean>('0'))
Optic.id<ST, TT>().compose(Optic.at<ST, '0', boolean>('0'))

// $ExpectType PolyLens<S, { readonly a: boolean; readonly b: number; }, string, boolean>
Optic.key<S, 'a', boolean>('a')
Optic.at<S, 'a', boolean>('a')

// $ExpectType PolyLens<ST, readonly [boolean, number], string, boolean>
Optic.key<ST, '0', boolean>('0')
Optic.at<ST, '0', boolean>('0')

//
// filter
//

declare const isString: (u: unknown) => u is string

// $ExpectType Prism<string | number, string>
Optic.id<string | number>().compose(Optic.filter(isString))

declare const predicate: (u: string | number | boolean) => boolean

// $ExpectType Prism<string | number, string | number>
Optic.id<string | number>().compose(Optic.filter(predicate))
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ export const cons: {
*/
export const nonNullable = <S>(): Prism<S, NonNullable<S>> =>
prism(
(s) => s == null ? E.left(new Error(`${s} did not satisfy isNonNullable`)) : E.right(s),
(s) => s == null ? E.left(new Error(`${s} did not satisfy isNonNullable`)) : E.right(s as any),
identity
)

Expand All @@ -414,8 +414,8 @@ export const nonNullable = <S>(): Prism<S, NonNullable<S>> =>
* @since 1.0.0
*/
export const filter: {
<S, A extends S>(refinement: Refinement<S, A>): Prism<S, A>
<S>(predicate: Predicate<S>): Prism<S, S>
<S extends A, B extends A, A = S>(refinement: Refinement<A, B>): Prism<S, B>
<S extends A, A = S>(predicate: Predicate<A>): Prism<S, S>
} = <S>(predicate: Predicate<S>): Prism<S, S> =>
prism(
(s) =>
Expand Down

0 comments on commit 2209531

Please sign in to comment.