diff --git a/packages/flow-runtime/src/registerTypePredicates.js b/packages/flow-runtime/src/registerTypePredicates.js index beefe65b..1e958185 100644 --- a/packages/flow-runtime/src/registerTypePredicates.js +++ b/packages/flow-runtime/src/registerTypePredicates.js @@ -2,9 +2,15 @@ import type TypeContext from './TypeContext'; +const objectToString = Object.prototype.toString; + +const isMap = (input: any): boolean => { + return input instanceof Map || objectToString.call(input) === '[object Map]'; +}; + export default function registerTypePredicates (context: TypeContext) { context.setPredicate('Array', (input: any) => Array.isArray(input)); - context.setPredicate('Map', (input: any) => input instanceof Map); + context.setPredicate('Map', isMap); context.setPredicate('Set', (input: any) => input instanceof Set); context.setPredicate('Promise', (input: any) => { if (input instanceof Promise) { diff --git a/packages/flow-runtime/src/typed.test.js b/packages/flow-runtime/src/typed.test.js index d2eb3380..e10133be 100644 --- a/packages/flow-runtime/src/typed.test.js +++ b/packages/flow-runtime/src/typed.test.js @@ -3,6 +3,8 @@ import {ok, equal, throws} from 'assert'; import t from './globalContext'; +const BabelRuntimeMap: any = require('babel-runtime/core-js/map').default; // eslint-disable-line import/no-extraneous-dependencies + const no = (input: any): any => equal(Boolean(input), false); describe('Typed API', () => { @@ -259,6 +261,22 @@ describe('Typed API', () => { }); + it('should accept a babel-runtime Map', () => { + const RuntimeMap = BabelRuntimeMap; + const type = t.ref(Map, t.string(), t.number()); + + const valid = new RuntimeMap([ + ['valid', 123] + ]); + ok(type.accepts(valid)); + + const invalid = new RuntimeMap([ + ['valid', 123], + ['notvalid', false] + ]); + no(type.accepts(invalid)); + }); + it('should make a simple function type', () => { const type = t.fn( t.param('input', t.boolean()), @@ -558,4 +576,4 @@ describe('Typed API', () => { )) ); }); -}); \ No newline at end of file +});