As listed below, let's say we have:
- a function
rthat combines multiple tasks together - and a function
othat returns a shape likewhen(cb).map(cb)
every callback passed to when or map should always take these 3 arguments: S, A, C where S and C are defined in r and A is defined in o.
here is a link to the typescript playground which also shows the error I am getting.
My question is: How can I get a typesafe declaration?
type Task<S, A, C> = <AA extends A>(s: S, a: AA, c: C) => any;
type Fn<S, A, C> = (s: S, a: A, c: C) => any;
const r = <S, C>() => ({
tasks: (...tasks: Task<S, any, C>[]) => null,
});
const o = <T, A = { prop: T }>(type: T) => ({
when: <S, C>(fp: Fn<S, A, C>) => ({
map: <SS extends S, CC extends C>(fn: Fn<SS, A, CC>): Task<SS, A, CC> => (
(s: SS, a: A, c: CC): any => (
fp(s, a, c) ? fn(s, a, c) : s
)
),
}),
});
const result = r<2, 7>().tasks(
o(44) // expect: cb(2, 44, 7)
.when((s, a, c) => s + a.prop + c)
.map((s, a, c) => s + a.prop + c),
o(78) // expect: cb(2, 78, 7)
.when((s, a, c) => s + a.prop + c)
.map((s, a, c) => s + a.prop + c),
// etc...
// callback provided to `.map` is typesafe,
// callback provided to `.when` is not,
);
As you can see, the callback provided to when is not typesafe: Params S and C are lost.
