Skip to content

Commit

Permalink
Merge pull request #735 from bitgopatmcl/make-optionalized-work-like-…
Browse files Browse the repository at this point in the history
…partial

Make undefined behavior of optionalized work like partial
  • Loading branch information
andrew-scott-fischer authored Apr 10, 2024
2 parents 43f8823 + 2b12d17 commit 9308b94
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/io-ts-http/src/combinators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ const partialWithoutUndefined = <P extends t.Props>(
},
(a) => {
const result = partialCodec.encode(a);
if (result === undefined) {
// `t.partial` will return this when passed `undefined` even though it is not in the type
return result;
}
for (const key of Object.keys(result)) {
if (result[key] === void 0) {
delete result[key];
Expand Down
6 changes: 6 additions & 0 deletions packages/io-ts-http/test/combinators.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ describe('optionalized', () => {
assertEncodes(optionalCodec, { a: undefined, b: 'foo' }, expected);
});

it('returns undefined when encoding undefined', () => {
const optionalCodec = c.optionalized({});
const expected = undefined;
assertEncodes(optionalCodec, undefined, expected);
});

it('decodes explicit null properties', () => {
const nullCodec = c.optionalized({
a: t.null,
Expand Down

0 comments on commit 9308b94

Please sign in to comment.