Skip to content

Commit e086bec

Browse files
committed
Fix types
1 parent 65dee75 commit e086bec

File tree

3 files changed

+53
-7
lines changed

3 files changed

+53
-7
lines changed

.eslintrc.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,23 @@ module.exports = {
1515
'react-hooks/rules-of-hooks': 'error', // Checks rules of Hooks
1616
'react-hooks/exhaustive-deps': 'warn', // Checks effect dependencies
1717
},
18-
overrides: [
18+
overrides: [
1919
{
2020
// For react component files, we want to allow types of the form `type Props = {}`
2121
// as Props is guaranteed to be an object by React. Without overriding this rule,
2222
// we would need to extend the Props generic from Record<string, unknown>
2323
// which causes issues when consumer props are defined as interface without an index signature.
2424
// Refer to react part of the comment for more details.
2525
// https://github.com/typescript-eslint/typescript-eslint/issues/2063#issuecomment-675156492
26-
excludedFiles: "src/*.tsx",
26+
excludedFiles: 'src/*.tsx',
27+
files: ['*.tsx'],
2728
rules: {
28-
"@typescript-eslint/ban-types": [
29-
"error",
29+
'@typescript-eslint/ban-types': [
30+
'error',
3031
{
3132
extendDefaults: true,
3233
types: {
33-
"{}": false,
34+
'{}': false,
3435
},
3536
},
3637
],

example/src/TsTest.tsx

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
}

src/index.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ export const reducer = (
160160
};
161161

162162
// Get modal component by modal id
163-
function getModal(modalId: string) {
163+
function getModal(modalId: string): React.FC<any> | undefined {
164164
return MODAL_REGISTRY[modalId]?.comp;
165165
}
166166

@@ -223,6 +223,7 @@ export function show<T extends any, C extends any>(
223223
modal: React.FC<C>,
224224
args?: NiceModalArgs<React.FC<C>>,
225225
): Promise<T>;
226+
226227
// export function show<T extends any, C extends React.FC>(modal: C, args?: Omit<React.ComponentProps<C>, 'id'>): Promise<T>;
227228
export function show<T extends any>(modal: string, args?: Record<string, unknown>): Promise<T>;
228229
export function show<T extends any, P extends any>(modal: string, args: P): Promise<T>;
@@ -383,7 +384,7 @@ export function useModal(modal?: any, args?: any): any {
383384
],
384385
);
385386
}
386-
export const create = <P extends {}>(
387+
export const create = <P extends Record<string, unknown>>(
387388
Comp: React.ComponentType<P>,
388389
): React.FC<P & NiceModalHocProps> => {
389390
return ({ defaultVisible, keepMounted, id, ...props }) => {

0 commit comments

Comments
 (0)