From bcc227245fc5491c9944f4c85233e751ee5b9f9a Mon Sep 17 00:00:00 2001 From: Chris Wilkinson Date: Thu, 8 Sep 2022 14:47:12 +0100 Subject: [PATCH] Allow Reader's local to infer the environment type --- docs/modules/Reader.ts.md | 2 +- docs/modules/ReaderEither.ts.md | 4 ++- docs/modules/ReaderIO.ts.md | 2 +- docs/modules/ReaderTask.ts.md | 2 +- docs/modules/ReaderTaskEither.ts.md | 2 +- docs/modules/StateReaderTaskEither.ts.md | 2 +- dtslint/ts4.1/Reader.ts | 34 ++++++++++++++++++++++ dtslint/ts4.1/ReaderEither.ts | 34 ++++++++++++++++++++++ dtslint/ts4.1/ReaderIO.ts | 36 ++++++++++++++++++++++++ dtslint/ts4.1/ReaderTask.ts | 34 ++++++++++++++++++++++ dtslint/ts4.1/ReaderTaskEither.ts | 34 ++++++++++++++++++++++ dtslint/ts4.1/StateReaderTaskEither.ts | 34 ++++++++++++++++++++++ src/Reader.ts | 2 +- src/ReaderEither.ts | 2 +- src/ReaderIO.ts | 2 +- src/ReaderTask.ts | 2 +- src/ReaderTaskEither.ts | 2 +- src/StateReaderTaskEither.ts | 2 +- 18 files changed, 220 insertions(+), 12 deletions(-) create mode 100644 dtslint/ts4.1/ReaderIO.ts diff --git a/docs/modules/Reader.ts.md b/docs/modules/Reader.ts.md index aa369e311..193274c58 100644 --- a/docs/modules/Reader.ts.md +++ b/docs/modules/Reader.ts.md @@ -315,7 +315,7 @@ Changes the value of the local context during the execution of the action `ma` ( **Signature** ```ts -export declare const local: (f: (r2: R2) => R1) => (ma: Reader) => Reader +export declare const local: (f: (r2: R2) => R1) => (ma: Reader) => Reader ``` Added in v3.0.0 diff --git a/docs/modules/ReaderEither.ts.md b/docs/modules/ReaderEither.ts.md index a9a62fad9..b41f12343 100644 --- a/docs/modules/ReaderEither.ts.md +++ b/docs/modules/ReaderEither.ts.md @@ -531,7 +531,9 @@ Changes the value of the local context during the execution of the action `ma` ( **Signature** ```ts -export declare const local: (f: (r2: R2) => R1) => (ma: ReaderEither) => ReaderEither +export declare const local: ( + f: (r2: R2) => R1 +) => (ma: ReaderEither) => ReaderEither ``` Added in v3.0.0 diff --git a/docs/modules/ReaderIO.ts.md b/docs/modules/ReaderIO.ts.md index b43424fbd..facb237ac 100644 --- a/docs/modules/ReaderIO.ts.md +++ b/docs/modules/ReaderIO.ts.md @@ -387,7 +387,7 @@ Changes the value of the local context during the execution of the action `ma` ( **Signature** ```ts -export declare const local: (f: (r2: R2) => R1) => (ma: ReaderIO) => ReaderIO +export declare const local: (f: (r2: R2) => R1) => (ma: ReaderIO) => ReaderIO ``` Added in v3.0.0 diff --git a/docs/modules/ReaderTask.ts.md b/docs/modules/ReaderTask.ts.md index 626385097..6cbea40e2 100644 --- a/docs/modules/ReaderTask.ts.md +++ b/docs/modules/ReaderTask.ts.md @@ -338,7 +338,7 @@ Changes the value of the local context during the execution of the action `ma` ( **Signature** ```ts -export declare const local: (f: (r2: R2) => R1) => (ma: ReaderTask) => ReaderTask +export declare const local: (f: (r2: R2) => R1) => (ma: ReaderTask) => ReaderTask ``` Added in v3.0.0 diff --git a/docs/modules/ReaderTaskEither.ts.md b/docs/modules/ReaderTaskEither.ts.md index 8aa52e925..2c0449cea 100644 --- a/docs/modules/ReaderTaskEither.ts.md +++ b/docs/modules/ReaderTaskEither.ts.md @@ -880,7 +880,7 @@ Changes the value of the local context during the execution of the action `ma` ( **Signature** ```ts -export declare const local: ( +export declare const local: ( f: (r2: R2) => R1 ) => (ma: ReaderTaskEither) => ReaderTaskEither ``` diff --git a/docs/modules/StateReaderTaskEither.ts.md b/docs/modules/StateReaderTaskEither.ts.md index 9659dac96..fcd8e35e8 100644 --- a/docs/modules/StateReaderTaskEither.ts.md +++ b/docs/modules/StateReaderTaskEither.ts.md @@ -770,7 +770,7 @@ Changes the value of the local context during the execution of the action `ma` ( **Signature** ```ts -export declare const local: ( +export declare const local: ( f: (r2: R2) => R1 ) => (ma: StateReaderTaskEither) => StateReaderTaskEither ``` diff --git a/dtslint/ts4.1/Reader.ts b/dtslint/ts4.1/Reader.ts index 9c4e5ef78..a075c5da9 100644 --- a/dtslint/ts4.1/Reader.ts +++ b/dtslint/ts4.1/Reader.ts @@ -1,6 +1,40 @@ import * as _ from '../../src/Reader' import { pipe } from '../../src/function' +declare function modifyA(r: R): R + +// +// local +// + +// $ExpectType Reader<{ a: string; }, string> +pipe( + _.of('a'), + _.local((env) => ({ + a: env.a + })) +) + +// $ExpectType Reader<{ b: string; }, string> +pipe( + _.of('a'), + _.local((env: { b: string }) => ({ + a: env.b + })) +) + +// $ExpectType Reader<{ b: string; }, string> +pipe( + _.of('a'), + _.local((env: { b: string }) => ({ + ...env, + a: env.b + })) +) + +// $ExpectType Reader<{ a: string; b: string; }, string> +pipe(_.of('a'), _.local(modifyA)) + // // chainW // diff --git a/dtslint/ts4.1/ReaderEither.ts b/dtslint/ts4.1/ReaderEither.ts index c891dff41..3cefd6e99 100644 --- a/dtslint/ts4.1/ReaderEither.ts +++ b/dtslint/ts4.1/ReaderEither.ts @@ -3,6 +3,40 @@ import * as R from '../../src/Reader' import * as E from '../../src/Either' import { pipe } from '../../src/function' +declare function modifyA(r: R): R + +// +// local +// + +// $ExpectType ReaderEither<{ a: string; }, number, string> +pipe( + _.of('a'), + _.local((env) => ({ + a: env.a + })) +) + +// $ExpectType ReaderEither<{ b: string; }, number, string> +pipe( + _.of('a'), + _.local((env: { b: string }) => ({ + a: env.b + })) +) + +// $ExpectType ReaderEither<{ b: string; }, number, string> +pipe( + _.of('a'), + _.local((env: { b: string }) => ({ + ...env, + a: env.b + })) +) + +// $ExpectType ReaderEither<{ a: string; b: string; }, number, string> +pipe(_.of('a'), _.local(modifyA)) + // // getOrElseW // diff --git a/dtslint/ts4.1/ReaderIO.ts b/dtslint/ts4.1/ReaderIO.ts new file mode 100644 index 000000000..2b401cc9e --- /dev/null +++ b/dtslint/ts4.1/ReaderIO.ts @@ -0,0 +1,36 @@ +import * as _ from '../../src/ReaderIO' +import { pipe } from '../../src/function' + +declare function modifyA(r: R): R + +// +// local +// + +// $ExpectType ReaderIO<{ a: string; }, string> +pipe( + _.of('a'), + _.local((env) => ({ + a: env.a + })) +) + +// $ExpectType ReaderIO<{ b: string; }, string> +pipe( + _.of('a'), + _.local((env: { b: string }) => ({ + a: env.b + })) +) + +// $ExpectType ReaderIO<{ b: string; }, string> +pipe( + _.of('a'), + _.local((env: { b: string }) => ({ + ...env, + a: env.b + })) +) + +// $ExpectType ReaderIO<{ a: string; b: string; }, string> +pipe(_.of('a'), _.local(modifyA)) diff --git a/dtslint/ts4.1/ReaderTask.ts b/dtslint/ts4.1/ReaderTask.ts index 30638c12c..4fcc019c1 100644 --- a/dtslint/ts4.1/ReaderTask.ts +++ b/dtslint/ts4.1/ReaderTask.ts @@ -1,6 +1,40 @@ import * as _ from '../../src/ReaderTask' import { pipe } from '../../src/function' +declare function modifyA(r: R): R + +// +// local +// + +// $ExpectType ReaderTask<{ a: string; }, string> +pipe( + _.of('a'), + _.local((env) => ({ + a: env.a + })) +) + +// $ExpectType ReaderTask<{ b: string; }, string> +pipe( + _.of('a'), + _.local((env: { b: string }) => ({ + a: env.b + })) +) + +// $ExpectType ReaderTask<{ b: string; }, string> +pipe( + _.of('a'), + _.local((env: { b: string }) => ({ + ...env, + a: env.b + })) +) + +// $ExpectType ReaderTask<{ a: string; b: string; }, string> +pipe(_.of('a'), _.local(modifyA)) + // // Do // diff --git a/dtslint/ts4.1/ReaderTaskEither.ts b/dtslint/ts4.1/ReaderTaskEither.ts index 61688423d..239b5d481 100644 --- a/dtslint/ts4.1/ReaderTaskEither.ts +++ b/dtslint/ts4.1/ReaderTaskEither.ts @@ -5,6 +5,40 @@ import * as TE from '../../src/TaskEither' import * as IOE from '../../src/IOEither' import { pipe } from '../../src/function' +declare function modifyA(r: R): R + +// +// local +// + +// $ExpectType ReaderTaskEither<{ a: string; }, number, string> +pipe( + _.of('a'), + _.local((env) => ({ + a: env.a + })) +) + +// $ExpectType ReaderTaskEither<{ b: string; }, number, string> +pipe( + _.of('a'), + _.local((env: { b: string }) => ({ + a: env.b + })) +) + +// $ExpectType ReaderTaskEither<{ b: string; }, number, string> +pipe( + _.of('a'), + _.local((env: { b: string }) => ({ + ...env, + a: env.b + })) +) + +// $ExpectType ReaderTaskEither<{ a: string; b: string; }, number, string> +pipe(_.of('a'), _.local(modifyA)) + // // getOrElseW // diff --git a/dtslint/ts4.1/StateReaderTaskEither.ts b/dtslint/ts4.1/StateReaderTaskEither.ts index 6f78e7b85..69aac5151 100644 --- a/dtslint/ts4.1/StateReaderTaskEither.ts +++ b/dtslint/ts4.1/StateReaderTaskEither.ts @@ -5,6 +5,40 @@ import * as RTE from '../../src/ReaderTaskEither' import * as IOE from '../../src/IOEither' import { pipe } from '../../src/function' +declare function modifyA(r: R): R + +// +// local +// + +// $ExpectType StateReaderTaskEither +pipe( + _.of('a'), + _.local((env) => ({ + a: env.a + })) +) + +// $ExpectType StateReaderTaskEither +pipe( + _.of('a'), + _.local((env: { b: string }) => ({ + a: env.b + })) +) + +// $ExpectType StateReaderTaskEither +pipe( + _.of('a'), + _.local((env: { b: string }) => ({ + ...env, + a: env.b + })) +) + +// $ExpectType StateReaderTaskEither +pipe(_.of('a'), _.local(modifyA)) + // // chainW // diff --git a/src/Reader.ts b/src/Reader.ts index 09e017254..c31a89f77 100644 --- a/src/Reader.ts +++ b/src/Reader.ts @@ -76,7 +76,7 @@ export const asksReader: (f: (r: R) => Reader) => Reader = ask * @category combinators * @since 3.0.0 */ -export const local = (f: (r2: R2) => R1) => (ma: Reader): Reader => (r2) => ma(f(r2)) +export const local = (f: (r2: R2) => R1) => (ma: Reader): Reader => (r2) => ma(f(r2)) /** * `map` can be used to turn functions `(a: A) => B` into functions `(fa: F) => F` whose argument and return types diff --git a/src/ReaderEither.ts b/src/ReaderEither.ts index d3eb9ad86..27337ef79 100644 --- a/src/ReaderEither.ts +++ b/src/ReaderEither.ts @@ -230,7 +230,7 @@ export const toUnion: (fa: ReaderEither) => Reader = * @category combinators * @since 3.0.0 */ -export const local: (f: (r2: R2) => R1) => (ma: ReaderEither) => ReaderEither = +export const local: (f: (r2: R2) => R1) => (ma: ReaderEither) => ReaderEither = R.local /** diff --git a/src/ReaderIO.ts b/src/ReaderIO.ts index d6b2344fc..1561bb103 100644 --- a/src/ReaderIO.ts +++ b/src/ReaderIO.ts @@ -59,7 +59,7 @@ export const fromIO: FromIO2['fromIO'] = /*#__PURE__*/ R.of * @category combinators * @since 3.0.0 */ -export const local: (f: (r2: R2) => R1) => (ma: ReaderIO) => ReaderIO = R.local +export const local: (f: (r2: R2) => R1) => (ma: ReaderIO) => ReaderIO = R.local /** * Less strict version of [`asksReaderIO`](#asksreaderio). diff --git a/src/ReaderTask.ts b/src/ReaderTask.ts index 843e564cd..c12908768 100644 --- a/src/ReaderTask.ts +++ b/src/ReaderTask.ts @@ -100,7 +100,7 @@ export const fromIO: FromIO2['fromIO'] = * @category combinators * @since 3.0.0 */ -export const local: (f: (r2: R2) => R1) => (ma: ReaderTask) => ReaderTask = R.local +export const local: (f: (r2: R2) => R1) => (ma: ReaderTask) => ReaderTask = R.local /** * `map` can be used to turn functions `(a: A) => B` into functions `(fa: F) => F` whose argument and return types diff --git a/src/ReaderTaskEither.ts b/src/ReaderTaskEither.ts index 48f94905c..15a2f8757 100644 --- a/src/ReaderTaskEither.ts +++ b/src/ReaderTaskEither.ts @@ -369,7 +369,7 @@ export const chainNullableK: ( * @category combinators * @since 3.0.0 */ -export const local: ( +export const local: ( f: (r2: R2) => R1 ) => (ma: ReaderTaskEither) => ReaderTaskEither = R.local diff --git a/src/StateReaderTaskEither.ts b/src/StateReaderTaskEither.ts index 29815d42a..37fadf5c6 100644 --- a/src/StateReaderTaskEither.ts +++ b/src/StateReaderTaskEither.ts @@ -248,7 +248,7 @@ export const fromReaderTaskEither: NaturalTransformation34 = * @category combinators * @since 3.0.0 */ -export const local = (f: (r2: R2) => R1) => ( +export const local = (f: (r2: R2) => R1) => ( ma: StateReaderTaskEither ): StateReaderTaskEither => flow(ma, R.local(f))