Skip to content
Advertisement

How can I do a boolean | undefined vs boolean | null PropType?

I have:

JavaScript

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

or you can also use ??:

JavaScript

Option 2: Omit & intersect with correct type:

JavaScript

Option 3: Change the definition of ModalProps:

JavaScript

Option 4: Ignore TypeScript:

JavaScript

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.

Playground with all the options

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement