TS错误:类型的参数无法分配给类型错误的参数

时间:2020-01-10 05:18:27

标签: typescript

我的应用程序中的打字功能遇到困难。

在我的传奇故事中,我打了这个put电话:

  yield put(getConsultationAttachments({
    id: action.payload.data.data.id,
    type: queryType
  }));

put类型是这样的:

export function put<A extends Action>(action: A): PutEffect<A>

getConsultationAttachments是一个动作创建者,看起来像这样:

const getConsultationAttachments = (id, type) => 
  getEntityAttachments(id, type, GET_CONSULTATION_ATTACHMENTS_TYPES);

getEntityAttachments是另一个动作创建者,如下所示。 它返回一个具有types属性而不是type属性的操作。这是因为我的应用程序中存在传奇的中间件。

它与佐贺的put效果并不完美,因为它期望使用type属性,而不是types

const getEntityAttachments = (
  id: number,
  attachmentType: string,
  types: string[]
) => ({
  types: types,
  payload: {
    request: {
      url: '/api/${id}?type=${attachmentType}'
    }
  }
});

我正在尝试为这些函数编写类型,但遇到TS错误。

这是我的类型:

interface Action<T = any> {
  type: T
}

type Actions = {
  types: string[];
  payload: any;
  meta?: any;
} & Action;

type GetConsultationAttachmentsProps = {
  id: number;
  type: 'string';
};
type GetConsultationAttachmentsFunc = ({
 id,
 type
}: GetConsultationAttachmentsProps) => Action & Actions;

type getEntityAttachmentsFunc = (
  id: number,
  attachmentType: any,
  types: string[]
) => Action;

然后我重写了函数,但仍然出现TS错误:

错误:(277,3)TS2322:类型'{类型:字符串[];有效负载:{请求:{网址:字符串; }; }; }”不可分配给“ Action&{类型:字符串[];有效负载:任何;元吗? }&Action”。 类型'{type:string [];中缺少属性'type'。有效负载:{请求:{网址:字符串; }; }; }”,但在“操作”类型中为必填项。

const getConsultationAttachments: GetConsultationAttachmentsFunc = 
  ({ id, type }) =>
    getEntityAttachments(id, type, GET_CONSULTATION_ATTACHMENTS_TYPES); // <= error for this line

const getEntityAttachments: getEntityAttachmentsFunc = (
  id,
  attachmentType,
  types
) => ({ // <== error for the following block
  types: types,
  payload: {
    request: {
      url: '/api/${id}?type=${attachmentType}'
    }
  }
});

如何为这套函数编写类型?

0 个答案:

没有答案