typescript实现类型推算

在代码中我们需要实现condition和examineState这两个类型可填,我们也可以通过这种方式去实现 但是在字段很多而且在应用场景很多的时候这样写很麻烦也可能你这个地方需要必填另外一个地方不需要必填这样就造成了全局很多的重复代码。而且也不便于维护。

export type ContractParams = {

current: number;

pageSize: number;

phone: string;

condition?: string;

examineState?: string;

};

我们可以通过Omit通过传入参数去剔除我们非必填项,然后通过Pick选取提出的非必填项在使用typeScript的partial函数变成非必填然后使用&把两个type合并成一个。这样的函数在全局就可以直接使用也不会造成重复的typeScript代码。

export type ContractParams = {

current: number;

pageSize: number;

phone: string;

condition: string;

examineState: string;

};

type Optional = Omit & Partial>;

export type ContractIteme = Optional;

精彩文章

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: