Skip to content

Commit

Permalink
🏷️ Fix object types, bump version to 1.0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
sproott committed May 8, 2021
1 parent ddacf3e commit 2e4591a
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "schemawax",
"version": "1.0.2",
"version": "1.0.3",
"description": "Tiny typed data decoder",
"homepage": "https://github.com/michaljanocko/schemawax",
"repository": "[email protected]:michaljanocko/schemawax.git",
Expand Down
8 changes: 4 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ export const keyValuePairs = <D>(decoder: Decoder<D>): Decoder<Array<[string, D]
//

type DecoderRecord = Record<PropertyKey, Decoder<any>>
type WithoutPartialUnknown<T> = T extends infer U & Partial<unknown> ? U : never
type OmitEmptyPartial<T extends DecoderRecord> = T extends infer U & Partial<{ [x: string]: any }> ? U : never
type ObjectType<D extends DecoderRecord> = D extends { [K in keyof infer U]: Decoder<(infer U)[K]> } ? U : never

const required = <D extends DecoderRecord>(
Expand Down Expand Up @@ -235,17 +235,17 @@ export const object = <D extends DecoderRecord, E extends DecoderRecord>(
required?: D
optional?: E
}
): Decoder<WithoutPartialUnknown<ObjectType<D> & Partial<ObjectType<E>>>> => createDecoder({
): Decoder<OmitEmptyPartial<ObjectType<D> & Partial<ObjectType<E>>>> => createDecoder({
forceDecode: (data) => {
checkDefined(data)

const result: Partial<WithoutPartialUnknown<ObjectType<D> & Partial<ObjectType<E>>>> = {}
const result: Partial<OmitEmptyPartial<ObjectType<D> & Partial<ObjectType<E>>>> = {}
if (struct.required !== undefined) {
Object.assign(result, required(struct.required).forceDecode(data))
}
if (struct.optional !== undefined) {
Object.assign(result, partial(struct.optional).forceDecode(data))
}
return result as WithoutPartialUnknown<ObjectType<D> & Partial<ObjectType<E>>>
return result as OmitEmptyPartial<ObjectType<D> & Partial<ObjectType<E>>>
}
})

0 comments on commit 2e4591a

Please sign in to comment.