Skip to content

Commit 9308b94

Browse files
Merge pull request #735 from bitgopatmcl/make-optionalized-work-like-partial
Make undefined behavior of optionalized work like partial
2 parents 43f8823 + 2b12d17 commit 9308b94

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

packages/io-ts-http/src/combinators.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ const partialWithoutUndefined = <P extends t.Props>(
4343
},
4444
(a) => {
4545
const result = partialCodec.encode(a);
46+
if (result === undefined) {
47+
// `t.partial` will return this when passed `undefined` even though it is not in the type
48+
return result;
49+
}
4650
for (const key of Object.keys(result)) {
4751
if (result[key] === void 0) {
4852
delete result[key];

packages/io-ts-http/test/combinators.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,12 @@ describe('optionalized', () => {
113113
assertEncodes(optionalCodec, { a: undefined, b: 'foo' }, expected);
114114
});
115115

116+
it('returns undefined when encoding undefined', () => {
117+
const optionalCodec = c.optionalized({});
118+
const expected = undefined;
119+
assertEncodes(optionalCodec, undefined, expected);
120+
});
121+
116122
it('decodes explicit null properties', () => {
117123
const nullCodec = c.optionalized({
118124
a: t.null,

0 commit comments

Comments
 (0)