Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion packages/flow-runtime/src/registerTypePredicates.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
20 changes: 19 additions & 1 deletion packages/flow-runtime/src/typed.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -259,6 +261,22 @@ describe('Typed API', () => {

});

it('should accept a babel-runtime Map<string, number>', () => {
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()),
Expand Down Expand Up @@ -558,4 +576,4 @@ describe('Typed API', () => {
))
);
});
});
});