diff --git a/CHANGELOG.md b/CHANGELOG.md index 7990f6787..6c04639e9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,11 @@ **Note**: Gaps between patch versions are faulty/broken releases. **Note**: A feature tagged as Experimental is in a high state of flux, you're at risk of it changing without notice. +# 2.2.13 + +- **Bug Fix** + - improve internal `mergeAll` function, closes #532 (@gcanti) + # 2.2.12 - **Experimental** diff --git a/package.json b/package.json index 89b653ff1..22fbf4851 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "io-ts", - "version": "2.2.12", + "version": "2.2.13", "description": "TypeScript runtime type system for IO decoding/encoding", "main": "lib/index.js", "module": "es6/index.js", diff --git a/src/index.ts b/src/index.ts index 10903a67e..c2204bfbd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1416,9 +1416,13 @@ export interface IntersectionC]> unknown > {} -const mergeAll = (base: any, us: Array): any => { +/** + * @internal + */ +export const mergeAll = (base: any, us: Array): any => { let equal = true let primitive = true + const baseIsNotADictionary = !UnknownRecord.is(base) for (const u of us) { if (u !== base) { equal = false @@ -1435,7 +1439,7 @@ const mergeAll = (base: any, us: Array): any => { let r: any = {} for (const u of us) { for (const k in u) { - if (u[k] !== base[k] || !r.hasOwnProperty(k)) { + if (!r.hasOwnProperty(k) || baseIsNotADictionary || u[k] !== base[k]) { r[k] = u[k] } } diff --git a/test/2.1.x/intersection.ts b/test/2.1.x/intersection.ts index 5a0e2b2b8..cd2515091 100644 --- a/test/2.1.x/intersection.ts +++ b/test/2.1.x/intersection.ts @@ -3,6 +3,13 @@ import * as t from '../../src/index' import { assertFailure, assertStrictEqual, assertSuccess, NumberFromString } from './helpers' describe('intersection', () => { + it('mergeAll', () => { + assert.deepStrictEqual(t.mergeAll(undefined, [{ prop1: 'b', prop2: 2 }, { prop1: 'a' }, { prop2: 1 }]), { + prop1: 'a', + prop2: 1 + }) + }) + describe('name', () => { it('should assign a default name', () => { const T = t.intersection([t.type({ a: t.string }), t.type({ b: t.number })])