|
| 1 | +// This a test file to check if typescript is working properly |
| 2 | + |
| 3 | +import NiceModal, { useModal, antdModalV5 } from '@ebay/nice-modal-react'; |
| 4 | + |
| 5 | +const MyModal1 = NiceModal.create(({ p1, p2 }: { p1: string; p2: number }) => { |
| 6 | + const modal = useModal(); |
| 7 | + return ( |
| 8 | + <div {...antdModalV5(modal)}> |
| 9 | + <h1>Foo</h1> |
| 10 | + <button onClick={modal.hide}>Close</button> |
| 11 | + </div> |
| 12 | + ); |
| 13 | +}); |
| 14 | + |
| 15 | +const MyModal2 = NiceModal.create(() => { |
| 16 | + const modal = useModal(); |
| 17 | + return ( |
| 18 | + <div {...antdModalV5(modal)}> |
| 19 | + <h1>Foo</h1> |
| 20 | + <button onClick={modal.hide}>Close</button> |
| 21 | + </div> |
| 22 | + ); |
| 23 | +}); |
| 24 | + |
| 25 | +NiceModal.register('modal-1', MyModal1, { p2: 1 }); |
| 26 | + |
| 27 | +export default function TsTest() { |
| 28 | + const modal1 = useModal(MyModal1, { p3: 'foo', p2: 123 }); |
| 29 | + modal1.show({ p1: 'foo', p2: 123, p4: 'hello' }); // expected: p4 should not be accepted |
| 30 | + |
| 31 | + NiceModal.show(MyModal1); // valid? |
| 32 | + NiceModal.show(MyModal1, { p1: 'foo', p2: 123 }); // valid |
| 33 | + NiceModal.show(MyModal1, { p1: 'foo', p2: '123' }); // expected ts error: p2 should be number |
| 34 | + NiceModal.show(MyModal1, { p1: 'foo' }); // unexpected ts error: p2 is already set when register |
| 35 | + |
| 36 | + const modal1_1 = useModal('modal-1', { p3: 'foo', p2: 123 }); |
| 37 | + modal1_1.show(); |
| 38 | + |
| 39 | + const modal2 = useModal(MyModal2); |
| 40 | + modal2.show(); |
| 41 | + modal2.show({ p1: 'foo', p2: 123 }); // expected ts error |
| 42 | + |
| 43 | + return <>hello ts</>; |
| 44 | +} |
0 commit comments