hoeck / simple-runtypes

Small, efficient and extendable runtype library for Typescript
MIT License
114 stars 5 forks source link

Optional in record #19

Closed pabra closed 3 years ago

pabra commented 3 years ago

I don't like the fix of the record related runtype (omit, pick, intersection) of the last commit. Any ideas? What do you think overall?

fixes #18

hoeck commented 3 years ago

Thanks!

Looks not that complicated but I probably would implement it differently, e.g. defining the record runtype as:

export function record<T extends object>(
  typemap: { [K in keyof T]: Runtype<T[K]> | Optional<T> },
): Runtype<OptionalRecord<T>> {

with function optional<T>(t: T): Optional<T>.

But I'm not sure what that implies - will have to compare both approaches and see which works best.

pabra commented 3 years ago

I don't get the typemap type of { [K in keyof T]: Runtype<T[K]> | Optional<T> } to work. Would be nice to see what you had in mind.

hoeck commented 3 years ago

Thanks for providing this PR as a reference. I've now implemented optional by using a mechanism similar to type-fests conditionalExcept and so I don't need the explicit optional symbol and omit and pick continue to work without modifying their return types.