Suppose I have this type:
type NS = {
readonly A: 4;
readonly B: 4;
readonly Op: 2;
readonly Cin: 1;
}
Then how can I statically know, with a type, that the sum of these ints is 11? I.e.,
type S = SumUp<NS> // how to implement SumUp so that S is 11?
I am using ts-arithmetic, which provides a handy Add<N, M> helper type, but I can't seem to find a way to recursively loop over all keys of NS and accumulate the results.
If you don't have that many keys, you could always hardcode the adds,
otherwise, you may have to convert a union to a tuple, from this question. Here's one such implementation using an accumulator (it's faster because it is tail call optimized):
Internally, we "loop over" each of the keys and add the value to the result. Then the
SumUptype is really just a wrapper around this internal helper, giving itTand its keys as a tuple.If this doesn't fancy you because of the use of
TuplifyUnion(and all its problems as described in the linked question), you could still do it:However, TS will generate an error, which I have not found a way around yet. So I have just resorted to ignoring it since it works for the inputs we give. This method is also a lot slower for TS to compute the result of (it is noticeably slower with a large delay).
Playground (hardcoded)
Playground (using TuplifyUnion)
Playground (no TuplifyUnion)