I have this type (come from third party library):
type StackActionType = {
type: 'REPLACE';
payload: {
name: string;
key?: string | undefined;
params?: object;
};
source?: string;
target?: string;
} | {
type: 'PUSH';
payload: {
name: string;
params?: object;
};
source?: string;
target?: string;
} | {
type: 'POP';
payload: {
count: number;
};
source?: string;
target?: string;
} | {
type: 'POP_TO_TOP';
source?: string;
target?: string;
};
I would like to infer remaining union types. I can achieve that with field key/name. ex:
const action: StackActionType = ...;
if("payload" in action) {
// action is infered with union types of: 'REPLACE', 'PUSH', 'POP' (contains "payload" field) and 'POP_TO_TOP' is omited
}
But there is a way to achieve that using field value instead of field name, ex:
if(["PUSH", "REPLACE"].includes(action.type)) {
// action should only infer union of 'REPLACE' & 'PUSH'. But this is not working...
}
Any idea (without copying/alter original type) ?
EDIT:
Here is a try with type guard & if: Typscript sandbox