typescript - 从数组中转移值的类型守卫
问题描述
const arr: number[] = []
const func = (n: number): void => {
console.log(n)
}
arr.push(222)
if (arr.length > 0) {
func(arr.shift())
}
我想func
用一个数字作为参数来调用。但是,我在 IDE 中收到此错误消息:
'number | 类型的参数 undefined' 不能分配给“number”类型的参数。类型“未定义”不可分配给类型“数字”.ts(2345)
如何在不将参数形式设置为 的情况下解决此n: number
问题n: number | undefined
?
解决方案
推荐阅读
- recursion - 是否可以将函数定义为自身?
- python - 使用 Luigi 包创建和运行任务
- sql - 通过 SQL 数据库重置 Azure MFA
- angular - 如何从具有角度 7 的引导日期选择器输入中获取值
- ruby-on-rails - 导入 React 库 Ruby On Rails
- amazon-web-services - 使用 aws sts 假设角色配置 AWS EMR spark
- jquery - 引导多选列表不会为 Rails 打开
- sql-server - 从sql存储过程下载原始pdf文件时如何处理pdf?
- vb.net - 禁用密码套件后,httpclient 在 Windows 7 中不起作用
- ansible - 使用多个主机进行部署时,如何循环访问不同的变量?