From 7a1b6ec81bbf2e1cbfc713fedde764bedcfbbf28 Mon Sep 17 00:00:00 2001 From: Atlas Date: Fri, 17 Apr 2026 15:07:11 +0800 Subject: [PATCH] fix: avoid throwing in warn mode for invalid types --- .../flow-runtime/src/flowTypes/$ShapeType.js | 5 +++- packages/flow-runtime/src/typed.test.js | 30 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/packages/flow-runtime/src/flowTypes/$ShapeType.js b/packages/flow-runtime/src/flowTypes/$ShapeType.js index 326c927..9a0b1ce 100644 --- a/packages/flow-runtime/src/flowTypes/$ShapeType.js +++ b/packages/flow-runtime/src/flowTypes/$ShapeType.js @@ -26,7 +26,10 @@ export default class $ShapeType extends Type<$Shape> { } type = type.unwrap(); - invariant(typeof type.getProperty === 'function', 'Can only $Shape object types.'); + if (typeof type.getProperty !== 'function') { + yield [path, 'Can only $Shape object types.', this]; + return; + } for (const key in input) { // eslint-disable-line guard-for-in const property = type.getProperty(key); diff --git a/packages/flow-runtime/src/typed.test.js b/packages/flow-runtime/src/typed.test.js index d2eb338..c1d68ff 100644 --- a/packages/flow-runtime/src/typed.test.js +++ b/packages/flow-runtime/src/typed.test.js @@ -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 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 object types\./); + }); + it('should $Keys', () => { const A = t.object( t.property('name', t.string()),