gcanti / io-ts

Runtime type system for IO decoding/encoding
https://gcanti.github.io/io-ts/
MIT License
6.71k stars 328 forks source link

Recursive Partial type in io-ts? #465

Open andrewconner opened 4 years ago

andrewconner commented 4 years ago

Consider this definition of RecursivePartial:

type RecursivePartial<T> = {
    [P in keyof T]?: RecursivePartial<T[P]>;
};

Is there a way to model this in io-ts? I have a large, nested record which is built incrementally over time. I'd like to model the "complete" type in io-ts, but be able to validate the recursive Partial (i.e., any key can be missing, and any provided key which is a record can have missing keys, etc).

Thanks!

JensForstmann commented 4 years ago

I'm also looking in this issue.

@andrewconner Have you been able to find a workaround for that?

andrewconner commented 4 years ago

I'm also looking in this issue.

@andrewconner Have you been able to find a workaround for that?

No, unfortunately not. My solution was to just maintain two separate entire types in code; i.e., C (complete) and P (partial). To ensure they stay together, I have a "type proof" function, which is never called, that asserts that all C's are P's, and all P's are RecursivePartial<C>'s (as defined above). The existence of this function proves at compile time that I didn't screw up a field somewhere.