我想在一个函数中定义具有不同形式的接口的有效负载。
每个有效负载如下,我已经定义了“ UpdatePayload”接口,如下所示:
/*
ex) payload interface
const interface updateBodyPayload = {
title: ...
content: ...
}
*/
/*
ex) payload interface
const interface updateStatePayload = {
type: ..
count: ..
}
*/
// Payload Merge interface
export type updatePayload = Partial<updateBodyPayload | updateStatePayload>;
但是,函数内部的代码产生以下错误:
// function.ts
const exec = (body: updatePayload) => {
const { title, content, type, count } = body;
// -> property 'title', ... does not exists on type updatePayload
}
我犯了什么错?
如果您想updatePayload
输入type是合并formupdateBodyPayload
和的结果updateStatePayload
,那么它必须类似于:(&
而不是|
)
type updatePayload = Partial<updateBodyPayload & updateStatePayload>;
现在updatePayload
将像:
type updatePayload = {
title?: string;
content?: string;
type?: string;
count?: number;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句