My function getRespectiveConfig()
returns a Union type that includes all the necessary types:
interface AlphaConfig {
title: string;
}
interface BetaConfig {
id: string;
}
export interface EncompassingConfig {
alphaConfig?: AlphaConfig;
betaConfig?: BetaConfig;
}
type ValueOf<T> = T[keyof T];
type RespectiveConfig = ValueOf<EncompassingConfig>; // *I may need to change this
export const getRespectiveConfig = (
nodes: Node[] | undefined,
): RespectiveConfig | undefined => {
return determineAndReturnRespectiveConfigType(nodes); // Assuming this returns AlphaConfig or BetaConfig
};
Explanation on how Modal
below receives the prop currentlyEditingConfig
:
interface Props {
currentlyEditingConfig: BetaConfig | undefined;
}
Here is how the issue arises. currentlyEditingConfig
expects to receive BetaConfig
which is one of the possible types in RespectiveConfig
. The error "Property 'id' is missing in type 'AlphaConfig' but required in type 'BetaConfig'." occurs:
<Modal
currentlyEditingConfig={state.nodes ? getRespectiveConfig(state.nodes) : undefined}
/>
I suspect the issue lies in the first code block where I have marked *
, I have attempted to use variations of Pick<K, T>
but have been unsuccessful in resolving it.
Useful threads:
- Turn typescript interface property types into union
- How to pass optional parameters while enforcing one to be passed in TypeScript?