Skip to content

Commit 590497a

Browse files
author
Elias Mulhall
committed
Disable incorrect tslint warning
TSLint reports that the result of a decoded object field can't be undefined in this situation. The result can definitely be undefined when the object field is optional and not present.
1 parent 2bbb592 commit 590497a

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

src/decoder.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ export class Decoder<A> {
241241
if (decoders.hasOwnProperty(key)) {
242242
const r = decoders[key].decode(json[key]);
243243
if (r.ok === true) {
244+
// tslint:disable-next-line:strict-type-predicates
244245
if (r.result !== undefined) {
245246
obj[key] = r.result;
246247
}

test/json-decode.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,16 @@ describe('object', () => {
251251
result: {a: true, b: 'hats'}
252252
});
253253
});
254+
255+
it('ignores optional fields that decode to undefined', () => {
256+
const decoder = object({
257+
a: number(),
258+
b: optional(string())
259+
});
260+
261+
expect(decoder.run({a: 12, b: 'hats'})).toEqual({ok: true, result: {a: 12, b: 'hats'}});
262+
expect(decoder.run({a: 12})).toEqual({ok: true, result: {a: 12}});
263+
});
254264
});
255265

256266
describe('array', () => {

0 commit comments

Comments
 (0)