The problem is TypeScript thinks the value I provide for the UiConfigContext.Provider
may be undefined. Probably because of the ?
config optional param however, I do provide the default { [Permission.Read]: true}
. I don't understand why it still thinks it could ever be undefined. I want this function's parameter to be optional so anything like that would work:
renderTestComponent();
renderTestComponent({ config: { [Permission.Read]: true });
renderTestComponent({ enterDetails: true });
renderTestComponent({ config: { [Permission.Read]: true }, enterDetails: true );
Here's my code (replicated error in TypeScript playground)
import React, { createContext } from 'react';
enum Permission {
Read = 'Read',
Write = 'Write',
}
type Configuration = Partial<Record<Permission, boolean>>;
export const UiConfigContext = createContext<Configuration | null>(null);
function renderTestComponent(args: {
config?: Configuration;
enterDetails?: boolean;
} = {
config: { [Permission.Read]: true},
enterDetails: false
}) {
<UiConfigContext.Provider value={args.config}>
Test
</UiConfigContext.Provider>
}