来自antd upload
的一个接口,如下:
interface UploadRequestOption<T = any> {
onProgress?: (event: UploadProgressEvent) => void;
onError?: (event: UploadRequestError | ProgressEvent, body?: T) => void;
onSuccess?: (body: T, xhr?: XMLHttpRequest) => void;
data?: Record<string, unknown>;
filename?: string;
file: Exclude<BeforeUploadFileType, File | boolean> | RcFile;
withCredentials?: boolean;
action: string;
headers?: UploadRequestHeader;
method: UploadRequestMethod;
}
我只需要保留file
项为必须属性,其他可选,我首先想到的是:
Partial<UploadRequestOption>
那这样file
就变成了可选,而Partial
好像并不能像Pick
那样能够接受第二个参数,如果要实现需求的话只能先把file拆出来,再合并成新的类型,如下:
type FileType = UploadRequestOption<ApiResponseType>['file'];
type OptionsType = Partial<Omit<UploadRequestOption<ApiResponseType>, 'file'>> & {
file: FileType;
};
请问还有更简化的方式吗?
完全可以自己封装个新的工具类型
使用: