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
5 changes: 4 additions & 1 deletion packages/flow-runtime/src/flowTypes/$ShapeType.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ export default class $ShapeType<T> extends Type<$Shape<T>> {
}

type = type.unwrap();
invariant(typeof type.getProperty === 'function', 'Can only $Shape<T> object types.');
if (typeof type.getProperty !== 'function') {
yield [path, 'Can only $Shape<T> object types.', this];
return;
}

for (const key in input) { // eslint-disable-line guard-for-in
const property = type.getProperty(key);
Expand Down
30 changes: 30 additions & 0 deletions packages/flow-runtime/src/typed.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,36 @@ describe('Typed API', () => {

});

it('should warn instead of throwing for invalid $Shape types in warn mode', () => {
const input = {foo: 'bar'};
const messages = [];
const originalMode = t.mode;
const originalEmitWarningMessage = t.emitWarningMessage;

t.mode = 'warn';
t.emitWarningMessage = (message) => {
messages.push(message);
};

try {
const invalidShape = t.$shape(t.string());
equal(t.check(invalidShape, input), input);
equal(messages.length, 1);
ok(messages[0].includes('Can only $Shape<T> object types.'));
}
finally {
t.mode = originalMode;
t.emitWarningMessage = originalEmitWarningMessage;
}
});

it('should still throw for invalid $Shape types in assert mode', () => {
const invalidShape = t.$shape(t.string());
throws(() => {
t.assert(invalidShape, {foo: 'bar'});
}, /Can only \$Shape<T> object types\./);
});

it('should $Keys<A>', () => {
const A = t.object(
t.property('name', t.string()),
Expand Down