Reversing From/To Types in a Schema
Hi there!
Is there a neat way to reverse the From/To types in a Schema?
I appreciate any help!
Here's what caused me to look for it
Is there a neat way to reverse the From/To types in a Schema?
const schemaWithTransform = S.struct({
NumberFromString: S.NumberFromString,
})
/*
type SchemaWithTransform: S.Schema<{
readonly NumberFromString: string;
}, {
readonly NumberFromString: number;
}>
*/
const reversedSchemaWithTransform = something
/*
type ReversedSchemaWithTransform: S.Schema<{
readonly NumberFromString: number;
}, {
readonly NumberFromString: string;
}>
*/const schemaWithTransform = S.struct({
NumberFromString: S.NumberFromString,
})
/*
type SchemaWithTransform: S.Schema<{
readonly NumberFromString: string;
}, {
readonly NumberFromString: number;
}>
*/
const reversedSchemaWithTransform = something
/*
type ReversedSchemaWithTransform: S.Schema<{
readonly NumberFromString: number;
}, {
readonly NumberFromString: string;
}>
*/I appreciate any help!
Here's what caused me to look for it
const SchemaWithTransform = S.struct({
NumberFromString: S.NumberFromString,
})
const SchemaWithImperativeLogic = S.transform(
S.unknown,
SchemaWithTransform, // Is there a way to reverse the From/To types here?
input => {
const res = S.parseEither(SchemaWithTransform)(input)
if (Either.isLeft(res)) {
throw new Error('wah')
}
return res.right as unknown as S.Schema.From<typeof SchemaWithTransform> // This actually returns the To type
},
identity,
)const SchemaWithTransform = S.struct({
NumberFromString: S.NumberFromString,
})
const SchemaWithImperativeLogic = S.transform(
S.unknown,
SchemaWithTransform, // Is there a way to reverse the From/To types here?
input => {
const res = S.parseEither(SchemaWithTransform)(input)
if (Either.isLeft(res)) {
throw new Error('wah')
}
return res.right as unknown as S.Schema.From<typeof SchemaWithTransform> // This actually returns the To type
},
identity,
)