Skip to content

Commit eedc197

Browse files
authored
relax UnknownRecord check, closes #559
1 parent cd5a1b0 commit eedc197

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/Guard.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export const UnknownArray: Guard<unknown, Array<unknown>> = {
9393
* @since 2.2.0
9494
*/
9595
export const UnknownRecord: Guard<unknown, Record<string, unknown>> = {
96-
is: (u: unknown): u is Record<string, unknown> => Object.prototype.toString.call(u) === '[object Object]'
96+
is: (u: unknown): u is Record<string, unknown> => u != null && typeof u === 'object' && !Array.isArray(u)
9797
}
9898

9999
// -------------------------------------------------------------------------------------

test/Guard.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as assert from 'assert'
22
import * as G from '../src/Guard'
33
import { pipe } from 'fp-ts/lib/pipeable'
4+
import { Stream } from 'stream'
45

56
interface NonEmptyStringBrand {
67
readonly NonEmptyString: unique symbol
@@ -281,4 +282,16 @@ describe('Guard', () => {
281282
assert.deepStrictEqual(guard.is({ _tag: 2, b: 'a' }), false)
282283
})
283284
})
285+
286+
describe('UnknownRecord', () => {
287+
it('should accept valid inputs', () => {
288+
assert.strictEqual(G.UnknownRecord.is(new Set()), true)
289+
assert.strictEqual(G.UnknownRecord.is(new Map()), true)
290+
assert.strictEqual(G.UnknownRecord.is(new Stream()), true)
291+
})
292+
293+
it('should reject invalid inputs', () => {
294+
assert.strictEqual(G.UnknownRecord.is([]), false)
295+
})
296+
})
284297
})

0 commit comments

Comments
 (0)