带条件(三元)运算符的TypeScript联合类型用法

带条件(三元)运算符的TypeScript联合类型用法,typescript,types,interface,union,Typescript,Types,Interface,Union,当使用两个自定义类型时,我收到一条神秘的错误消息。当使用两个自定义类型的“OptionsPostData3 | OptionsPostData4”临时名称时,我收到一条错误消息。当仅使用OptionsPostData3或OptionsPostData4时,我的代码的一半或另一半没有收到错误消息 我的代码是这样的 我没有幸运地尝试在网上找到一个解决方案,可能是显而易见的,也可能是在神秘的错误消息中找不到解决方案,但我不知道我应该怎么做 这些类型的定义如下 我认为错误消息可能提供足够的信息来解决我的

当使用两个自定义类型时,我收到一条神秘的错误消息。当使用两个自定义类型的“OptionsPostData3 | OptionsPostData4”临时名称时,我收到一条错误消息。当仅使用OptionsPostData3或OptionsPostData4时,我的代码的一半或另一半没有收到错误消息

我的代码是这样的 我没有幸运地尝试在网上找到一个解决方案,可能是显而易见的,也可能是在神秘的错误消息中找不到解决方案,但我不知道我应该怎么做

这些类型的定义如下 我认为错误消息可能提供足够的信息来解决我的问题,但我没有找到如何。。。我想避免使用any type关键字:

错误消息如下所示 如果需要完整项目作为参考

用于构建对象的接口与定义的接口不匹配。这两个选项是放松过滤器接口中的类型,或缩小参数类型以匹配过滤器中的类型,例如:添加默认值/消除未定义的可能性

接口过滤器{ underyingid?:编号; expiryDate?:字符串; optionType?:字符串|空; minstrikePrice?:数字|空; maxstrikePrice?:数字|空; }
你能分享完整的代码吗?underlyingId和OptionId等值设置为什么?完整代码作为我文章的最后一行提供:我在该页面上得到错误,而无需将任何值设置或分配给underlyingId作为数字或OptionId作为数字[]检查调用签名,大多数值与OptionPostData3中定义的值不匹配。例如,说underlyngid:number;但是签名说它可以是未定义的。尝试分配默认值或松开筛选器中的类型,以允许未定义、空、,etc@skovy奇怪的是,如果我说const postData:options postdata4=!!optionIds&&optionIds.length>0`。。。第一部分前半部分通过,或者如果我说'const postData:options postdata3=!!optionIds&&optionIds.length>0`。。。最后一部分是下半场
const postData: OptionsPostData3 | OptionsPostData4 =
      !!optionIds && optionIds.length > 0
        ? {
            optionIds,
          }
        : {
            filters: [
              {
                underlyingId,
                expiryDate,
                optionType: optionType || void 0,
                minstrikePrice: minstrikePrice || 0,
                maxstrikePrice: maxstrikePrice || 0,
              },
            ],
          };
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;
}
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)