gcanti / io-ts-types

A collection of codecs and combinators for use with io-ts
https://gcanti.github.io/io-ts-types/
MIT License
311 stars 40 forks source link

Something similar to AtLeastOne #128

Open CMCDragonkai opened 4 years ago

CMCDragonkai commented 4 years ago

🚀 Feature request

Something similar to:

type AtLeastOne<T> = { [K in keyof T]: Pick<T, K> }[keyof T]

Which is like partial, but ensures at least one property exists.

DenisFrezzato commented 4 years ago

What's the benefit of that type alias? It garantees that one property is defined, any of the properties, but without knowing which one. In practice, I don't see any difference between AtLeastOne<T> and Partial<T>, you still have to check if the property you need to deal with exists. Am I missing something?

As a side note, that type alias is not "at least", but "exactly one", because according to that type an object can't have more than one property. "At least one" should be modeled like this:

type AtLeastOne<T> = { [K in keyof T]: Partial<T> & Pick<T, K> }[keyof T]
CMCDragonkai commented 4 years ago

I use AtLeastOne in my sql update queries. And that type works for me. It enforces at least one and allows more than 1.