I receive a cryptic error message when using two custom Typecsipt types 'OptionsPostData3 | OptionsPostData4' (temporary name sorry) and a ternary operator when using only OptionsPostData3 or only OptionsPostData4 one half or the other half of my code is not getting an error message ...
My code look like this
const postData: OptionsPostData3 | OptionsPostData4 =
!!optionIds && optionIds.length > 0
? {
optionIds,
}
: {
filters: [
{
underlyingId,
expiryDate,
optionType: optionType || void 0,
minstrikePrice: minstrikePrice || 0,
maxstrikePrice: maxstrikePrice || 0,
},
],
};
I have not been lucky trying to find a solution online and probably it is obvious (or maybe not) to find a solution in the cryptic error message but I don't know what I should do ...
The types are defined like this
export type OptionsPostData3 = {
filters: Filters[];
};
export type OptionsPostData4 = { optionIds: number[] };
export interface OptionsIdArray {
optionIds: number[];
}
export interface FiltersArray {
filters: Filters[];
}
export interface Filters {
underlyingId: number;
expiryDate: string;
optionType: string;
minstrikePrice: number;
maxstrikePrice: number;
}
I think that the error message may provide enough information to solve my problem but I did not find how... And I want to avoid using the any type keyword :
The Error message looks like this
Type '{ optionIds: number[]; } | { filters: { underlyingId: number | undefined; expiryDate: string | undefined; optionType: string | undefined; minstrikePrice: number; maxstrikePrice: number; }[]; }' is not assignable to type 'OptionsPostData4 | OptionsPostData3'.
Type '{ filters: { underlyingId: number | undefined; expiryDate: string | undefined; optionType: string | undefined; minstrikePrice: number; maxstrikePrice: number; }[]; }' is not assignable to type 'OptionsPostData4 | OptionsPostData3'.
Type '{ filters: { underlyingId: number | undefined; expiryDate: string | undefined; optionType: string | undefined; minstrikePrice: number; maxstrikePrice: number; }[]; }' is not assignable to type 'OptionsPostData3'.
Types of property 'filters' are incompatible.
Type '{ underlyingId: number | undefined; expiryDate: string | undefined; optionType: string | undefined; minstrikePrice: number; maxstrikePrice: number; }[]' is not assignable to type 'Filters[]'.
Type '{ underlyingId: number | undefined; expiryDate: string | undefined; optionType: string | undefined; minstrikePrice: number; maxstrikePrice: number; }' is not assignable to type 'Filters'.
Types of property 'underlyingId' are incompatible.
Type 'number | undefined' is not assignable to type 'number'.
Type 'undefined' is not assignable to type 'number'.ts(2322)
if you need the full project as a reference this is the GitHub link, it starts on line 20
underlyingIdandoptionIdsset to?underlyingId as numberor tooptionIds as number[ ]OptionsPostData3. github.com/Luxcium/questrade-ts/blob/… For example, sayingunderlyingId: number;but the signature says it can be undefined. Try assigning defaults or loosening the types inFiltersto allowundefined,null, etc