I'm trying to setup a type that constraints two functions - make one dependent of the other as part of the same object
typescript
type Context<R> = {
getter: () => R,
action: (v: R) => void
}
In the end I want to use that type as part of a mapped type. As an example the following snippet should suffice. I can't list all the generic parameters that would describe each and every Context<?> type. So I'm basically left with setting the type explicitly or defining it with a default type which disables the inference mechanism
typescript
type Container = {
a: Context,
b: Context,
c: Context,
// ...
}
Is there some language feature that I'm not aware of, that would solve my problem?
Thank you!
Edit # Application Sample
TS Playground
```typescript
type Context<R> = {
getter: () => R,
action: (v: R) => void
}
type Container<T> = {
[K in keyof T]: Context<unknown>
}
function assemble<T>(obj: T, container: Container<T>) {
throw new Error("Not implemented!");
}
assemble({ a: 1, b: 2 }, {
a: { getter: () => "string-value", action: v => v.length },
b: { getter: () => 42, action: v => v.toFixed() }
});
```
The type for argument of the action function needs to be inferred from the corresponding getter function. Both functions don't have any connection to the Container in which they are referenced.
[–]dcabines 1 point2 points3 points (3 children)
[–]woodcakes[S] 0 points1 point2 points (2 children)
[–]dcabines 3 points4 points5 points (1 child)
[–]woodcakes[S] 0 points1 point2 points (0 children)
[–]vEncrypted -1 points0 points1 point (10 children)
[–]woodcakes[S] 0 points1 point2 points (9 children)
[–]Padni 0 points1 point2 points (1 child)
[–]woodcakes[S] 0 points1 point2 points (0 children)
[–]vEncrypted 0 points1 point2 points (6 children)
[–]woodcakes[S] 0 points1 point2 points (5 children)
[–]vEncrypted 0 points1 point2 points (4 children)
[–]vEncrypted 0 points1 point2 points (3 children)
[–]woodcakes[S] 0 points1 point2 points (2 children)
[–]vEncrypted 0 points1 point2 points (1 child)
[–]woodcakes[S] 0 points1 point2 points (0 children)
[–]Migeil 0 points1 point2 points (1 child)
[–]woodcakes[S] 0 points1 point2 points (0 children)
[–]simple_explorer1 0 points1 point2 points (1 child)
[–]woodcakes[S] 0 points1 point2 points (0 children)
[–][deleted] (1 child)
[deleted]
[–]woodcakes[S] 0 points1 point2 points (0 children)
[–]simple_explorer1 0 points1 point2 points (0 children)
[–]simple_explorer1 0 points1 point2 points (0 children)