I have a function getRespectiveConfig()
returning a Union type consisting of all the types I need to be return:
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>; // *Answer will probably require me to change this?
export const getRespectiveConfig = (
nodes: Node[] | undefined,
): RespectiveConfig | undefined => {
return determineAndReturnRespectiveConfigType(nodes); // Assume this returns type - AlphaConfig or BetaConfig
};
How Modal
below receives the prop currentlyEditingConfig
:
interface Props {
currentlyEditingConfig: BetaConfig | undefined;
}
Here is how it is used & where the issue lies. currentlyEditingConfig
is a prop expecting to receive BetaConfig
which is technically one of the possible types in RespectiveConfig
, I then receive an error that says "Property 'id' is missing in type 'AlphaConfig' but required in type 'BetaConfig'.":
<Modal
currentlyEditingConfig={state.nodes ? getRespectiveConfig(state.nodes) : undefined}
/>
I assume where I have marked *
in the first code block is where the issue would lie, I have tried to use variations of Pick<K, T>
but unable to figure it out.
Relevant threads: