Skip to content

Commit 2881d94

Browse files
committed
improve internal mergeAll function, closes #532
1 parent 1de671b commit 2881d94

File tree

4 files changed

+19
-3
lines changed

4 files changed

+19
-3
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
**Note**: Gaps between patch versions are faulty/broken releases.
1515
**Note**: A feature tagged as Experimental is in a high state of flux, you're at risk of it changing without notice.
1616

17+
# 2.2.13
18+
19+
- **Bug Fix**
20+
- improve internal `mergeAll` function, closes #532 (@gcanti)
21+
1722
# 2.2.12
1823

1924
- **Experimental**

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "io-ts",
3-
"version": "2.2.12",
3+
"version": "2.2.13",
44
"description": "TypeScript runtime type system for IO decoding/encoding",
55
"main": "lib/index.js",
66
"module": "es6/index.js",

src/index.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -1416,9 +1416,13 @@ export interface IntersectionC<CS extends [Mixed, Mixed, ...Array<Mixed>]>
14161416
unknown
14171417
> {}
14181418

1419-
const mergeAll = (base: any, us: Array<any>): any => {
1419+
/**
1420+
* @internal
1421+
*/
1422+
export const mergeAll = (base: any, us: Array<any>): any => {
14201423
let equal = true
14211424
let primitive = true
1425+
const baseIsNotADictionary = !UnknownRecord.is(base)
14221426
for (const u of us) {
14231427
if (u !== base) {
14241428
equal = false
@@ -1435,7 +1439,7 @@ const mergeAll = (base: any, us: Array<any>): any => {
14351439
let r: any = {}
14361440
for (const u of us) {
14371441
for (const k in u) {
1438-
if (u[k] !== base[k] || !r.hasOwnProperty(k)) {
1442+
if (!r.hasOwnProperty(k) || baseIsNotADictionary || u[k] !== base[k]) {
14391443
r[k] = u[k]
14401444
}
14411445
}

test/2.1.x/intersection.ts

+7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ import * as t from '../../src/index'
33
import { assertFailure, assertStrictEqual, assertSuccess, NumberFromString } from './helpers'
44

55
describe('intersection', () => {
6+
it('mergeAll', () => {
7+
assert.deepStrictEqual(t.mergeAll(undefined, [{ prop1: 'b', prop2: 2 }, { prop1: 'a' }, { prop2: 1 }]), {
8+
prop1: 'a',
9+
prop2: 1
10+
})
11+
})
12+
613
describe('name', () => {
714
it('should assign a default name', () => {
815
const T = t.intersection([t.type({ a: t.string }), t.type({ b: t.number })])

0 commit comments

Comments
 (0)