TS 关于泛类约束partial<T>

*首先,我们先通过一段代码来了解一下Partial<T>关键字

 *首先,我们定义了一个接口、一个函数、函数里面传了一个参数,参数用了partial<T>约束着,然后我们有写了四个测试的来调用这个函数,其中,前三个方法,没有报错,后面一个出现了报错,细心的人会发现没有报错的传的参数都是接口定义的有的,而报错的则相反,由此我们得出:

结论一:partial<T>:只要返回接口中定义已存在的就可以了

然后我们尝试将所传参数颠倒会发生报错吗?

 

但我们发现并没有;所以得出

结论2:partail<T>所传参数与顺序无关

以上两点,也是partial的基本使用方法

那么:他的原理是什么呢?让我们来看一段伪代码

 

 他先通过keyof来遍历Tode里面的键名,然后在通过in进行映射,然后赋值给P,而又通过Tode[P]取出中间的值,而中间的问号这个表示这个值可以传,也可以不传

代码的最终形态

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
类型“((config: AxiosRequestConfig<any>) => AxiosRequestConfig<any>) | undefined”的参数不能赋给类型“((value: InternalAxiosRequestConfig<any>) => InternalAxiosRequestConfig<any> | Promise<InternalAxiosRequestConfig<any>>) | null | undefined”的参数。 不能将类型“(config: AxiosRequestConfig<any>) => AxiosRequestConfig<any>”分配给类型“(value: InternalAxiosRequestConfig<any>) => InternalAxiosRequestConfig<any> | Promise<InternalAxiosRequestConfig<any>>”。 不能将类型“AxiosRequestConfig<any>”分配给类型“InternalAxiosRequestConfig<any> | Promise<InternalAxiosRequestConfig<any>>”。 不能将类型“AxiosRequestConfig<any>”分配给类型“InternalAxiosRequestConfig<any>”。 属性“headers”的类型不兼容。 不能将类型“AxiosHeaders | (Partial<RawAxiosHeaders & { Accept: AxiosHeaderValue; "Content-Length": AxiosHeaderValue; "User-Agent": AxiosHeaderValue; "Content-Encoding": AxiosHeaderValue; Authorization: AxiosHeaderValue; } & { ...; }> & Partial<...>) | undefined”分配给类型“AxiosRequestHeaders”。 不能将类型“undefined”分配给类型“AxiosRequestHeaders”。 不能将类型“undefined”分配给类型“Partial<RawAxiosHeaders & { Accept: AxiosHeaderValue; "Content-Length": AxiosHeaderValue; "User-Agent": AxiosHeaderValue; "Content-Encoding": AxiosHeaderValue; Authorization: AxiosHeaderValue; } & { ...; }>”。ts(2345) (property) HRequestInterceptors<AxiosResponse<any, any>>.requestInterceptors?: ((config: AxiosRequestConfig<any>) => AxiosRequestConfig<any>) | undefined
07-13
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值