I have:
JavaScript
x
18
18
1
const propTypes = {
2
// some other stuff
3
disableEscapeKeyDown: PropTypes.bool,
4
};
5
export type ModalProps = PropTypes.InferProps<typeof propTypes>;
6
7
export default function ModalWindow({
8
// other stuff
9
disableEscapeKeyDown = false,
10
}: ModalProps) {
11
const classes = useStyles({ width });
12
return (
13
<Modal
14
open={open ?? false}
15
onClose={onClose}
16
disableEscapeKeyDown={disableEscapeKeyDown}
17
>
18
But that gives me an error: Type 'boolean | null' is not assignable to type 'boolean | undefined'.
It seems that a MUI Modal takes boolean | undefined
whereas the PropType has it has boolean | null
. How can I reconcile that?
Advertisement
Answer
Option 1: Check for null
:
JavaScript
1
6
1
<Modal
2
open={open ?? false}
3
onClose={onClose}
4
disableEscapeKeyDown={disableEscapeKeyDown === null ? undefined : disableEscapeKeyDown}
5
>
6
or you can also use ??
:
JavaScript
1
2
1
disableEscapeKeyDown ?? undefined
2
Option 2: Omit & intersect with correct type:
JavaScript
1
4
1
export default function ModalWindow({
2
disableEscapeKeyDown = false,
3
}: Omit<ModalProps, "disableEscapeKeyDown"> & { disableEscapeKeyDown?: boolean }) {
4
Option 3: Change the definition of ModalProps
:
JavaScript
1
2
1
type ModalProps = PropTypes.InferProps<Omit<typeof propTypes, "disableEscapeKeyDown">> & { disableEscapeKeyDown?: boolean };
2
Option 4: Ignore TypeScript:
JavaScript
1
7
1
<Modal
2
open={open ?? false}
3
onClose={onClose}
4
//@ts-ignore
5
disableEscapeKeyDown={disableEscapeKeyDown}
6
>
7
Unfortunately, I don’t think you can “overwrite” or change how PropTypes.InferProps
works – even trying to redefine Requireable<T>
and Validator<T>
did not work (at least my attempts to do it). So here’s the next best thing: patching the results of the type yourself.