Proposal for Adding `decodePrimitives` Function for Enhanced Type Mapping

Would it make sense to add another decode function looking smthing like decodePrimitives .

It would be similar to decodeUnknown with the only difference lying in the input types not being
unknown
but being the primitives type of the expected output object.

When decoding data from 3rd party sources I mostly need to use decodeUnknown. However sometimes it's just a matter of mapping data shapes, or mapping an external
string
that is expected to be a union, a literal, enum or whatever. So I am confident enough to not pass an unknown value to the decodeUnknown, yet not confident enough to use the base decode.

Having another function that expects primitives would also improve DX to leverage IDE autocompletion with schema props.

What I ended up doing is creating a type called Primitives that I use like this:

export type MaybeOrder = Primitives<S.Schema.Encoded<typeof _Order>>


Which for instance generate something similar to :
(alias) type MaybeOrder = {
    readonly currency: string;
// ...

instead of
(alias) type Order = {
) currency: S.brand<S.Schema<"EUR" | "AED" | "AFN" | "XCD" | "ALL" | "AMD" | "ANG" | "AOA" | "USD" | "ARS" | "AUD" | "AWG" | "AZN" | "BAM" | "BBD" | "BDT" | "XOF" | "BGN"


And the I use it like this:
// ...

const maybeOrder: MaybeOrder = {
  /* ... */
};
return S.decodeUnknown(Order)(maybeOrder);


What's your opinion ?
Was this page helpful?