| 3772 | * @since 4.0.0 |
| 3773 | */ |
| 3774 | export function extendTo<S extends Struct<Struct.Fields>, const Fields extends Struct.Fields>( |
| 3775 | fields: Fields, |
| 3776 | derive: { readonly [K in keyof Fields]: (s: S["Type"]) => Option_.Option<Fields[K]["Type"]> } |
| 3777 | ) { |
| 3778 | return ( |
| 3779 | self: S |
| 3780 | ): decodeTo<Struct<Simplify<{ [K in keyof S["fields"]]: toType<S["fields"][K]> } & Fields>>, S> => { |
| 3781 | const f = Struct_.map(self.fields, toType) |
| 3782 | const to = Struct({ ...f, ...fields }) |
| 3783 | const keys = InternalRecord.ownEnumerableKeys(fields) |
| 3784 | return self.pipe(decodeTo( |
| 3785 | to, |
| 3786 | SchemaTransformation.transform({ |
| 3787 | decode: (input) => { |
| 3788 | const out: any = { ...input } |
| 3789 | for (const k of keys) { |
| 3790 | const f = derive[k] |
| 3791 | const o = f(input) |
| 3792 | if (Option_.isSome(o)) { |
| 3793 | InternalRecord.assignProperty(out, k, o.value) |
| 3794 | } |
| 3795 | } |
| 3796 | return out |
| 3797 | }, |
| 3798 | encode: (input) => { |
| 3799 | const out = { ...input } |
| 3800 | for (const k of keys) { |
| 3801 | delete out[k] |
| 3802 | } |
| 3803 | return out |
| 3804 | } |
| 3805 | }) |
| 3806 | )) as any |
| 3807 | } |
| 3808 | } |
| 3809 | /** |
| 3810 | * Namespace for `Record` type utilities. |
| 3811 | * |