TS2322: Type ‘null‘ is not assignable to type ‘File‘.

日常开发中对 File 声明属性的时候抛出了如下异常:

TS2322: Type 'null' is not assignable to type 'File'.

 

解决办法

方式一:

// @ts-ignore
fileToUpload: File = null;

方式二:

fileToUpload: File | null = null; // 个人推荐该方式

 

通过如上两种方式皆可实现,但个人更偏向于方式二,TS 的语法规范性真的好强 。。。
包括调用接口传参的时候,如果不对该 File 属性进行判空也会抛出异常 。。。

TS2345: Argument of type 'File | null' is not assignable to parameter of type 'File'.   Type 'null' is not assignable to type 'File'.

 

在调用接口方法前套一层 null 的判断即可。

if (this.fileToUpload !== null) {
    // do somethings
}

以上便是此次分享的全部内容,希望能对大家有所帮助!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值