当前位置:网站首页>TypeScript 错误 error TS2469、error TS2731 解决办法
TypeScript 错误 error TS2469、error TS2731 解决办法
2022-08-02 03:24:00 【varBlue】
前言
创建 Vue3 + TypeScript + vueRouter 项目中,在计算属性中使用 route.name 获取当前页面名称时,tsc 验证出现 error TS2469 和 error TS2731
src/components/xx.vue(xx,xx): error TS2469: The '+' operator cannot be applied to type 'symbol'.
src/components/xx.vue(xx,xx): error TS2731: Implicit conversion of a 'symbol' to a 'string' will fail at runtime. Consider wrapping this expression in 'String(...)'.
网上查找发现当前(20220728)并无相关解决方案,经排查,判断错误由 TypeScript 执行类型检查时可能将 route.name 类型推断为 symbol 引起,查看解决方法可直接跳转至“解决方案”
错误情况
1.错误来源
在 Vue 组合式 SFC 编写中,使用 computed() 函数设置计算属性,并拼接为需要的字符串样式
<script setup lang="ts">
import { computed } from 'vue';
import { useRoute } from 'vue-router';
const route = useRoute();
// 计算属性
const routerName = computed<string>(() => {
return ' - ' + route.name + ' Page';
});
</script>
这种情况下,TypeScript 执行类型检查,会出现 error TS2469 错误
src/components/xx.vue(xx,xx): error TS2469: The '+' operator cannot be applied to type 'symbol'.
如果将计算属性 return 部分写作以下样式
return ` - ${ route.name } Page`;
TypeScript 执行类型检查,会出现 error TS2731 错误
src/components/xx.vue(xx,xx): error TS2731: Implicit conversion of a 'symbol' to a 'string' will fail at runtime. Consider wrapping this expression in 'String(...)'.
2.原因查找
使用 typeof() 函数读取 route.name 的类型,返回 string
console.log(typeof(route.name)); // 返回 string
使用 TypeScript Playground 查看错误情况和类型推断,结果如下
symbol 类型是 ES6 新增的一种基本数据类型,用于避免 JavaScript 第三方插件、框架所定义的属性或方法被覆盖
结合 error TS2469 和 error TS2731 错误提示,判断出错原因为 TypeScript 将 route.name 的类型判断为 symbol
而 symbol 类型是不能与其它字符串进行拼接的,故而引发此错误
解决方法
将被 TypeScript 推断为 symbol 类型的 route.name 转换为 string 类型即可
使用 JavaScript 的 String() 函数进行类型转换
return ' - ' + String(route.name) + ' Page'; // TypeScript 验证通过
或使用 TypeScript 的 as 关键字进行类型断言
return ` - ${ route.name as string } Page`; // TypeScript 验证通过
边栏推荐
- Source Insight 使用教程(2)——常用功能
- 每日五道面试题总结 22/7/26
- Circular linked list---------Joseph problem
- require模块化语法
- Phospholipid-polyethylene glycol-targeted neovascularization targeting peptide APRPG, DSPE-PEG-APRPG
- 每日五道面试题总结 22/7/20
- [Learning Records of Boxue Valley] Super summary, share with heart | Software Testing Interface Testing Basics
- js的“类数组”及“类数组转数组”
- 阿里云设置域名解析重定向后,无法使用Chrome访问
- js scope and closure
猜你喜欢
微信小程序云开发-证件照的实现
DSPE-PEG-Silane, DSPE-PEG-SIL, phospholipid-polyethylene glycol-silane modified active group
如何计算地球上两点的距离(附公式推导)
js基础知识
我的小笔记 =》原生微信小程序
Living to detect the Adaptive Normalized Representation Learning for GeneralizableFace Anti - Spoofing reading notes
STM32 CAN过滤器
Deveco studio Hongmeng app access network detailed process (js)
C语言 0长度数组/柔性数组
6.27面试集
随机推荐
最新,每天填坑,Jeston TX1 精卫填坑,第一步:刷机
微信小程序云开发之券码领取,怎么防止用户领取到相同的数据?
第一次手撕代码,如何解出全排列问题
阿里云设置域名解析重定向后,无法使用Chrome访问
Small program van-cell line wrapping can be left-aligned
npm和package.json
如何计算地球上两点的距离(附公式推导)
C语言入门小游戏—三子棋
Guangzhou Huawei Interview Summary
语义分割标签即像素值的巨坑,transforms.ToTensor()的错误使用
DOM操作---放大镜案例
我的小笔记 =》其他东东
广州华为面试总结
微信小程序云开发如何将页面生成为pdf?
DSPE-PEG-DBCO Phospholipid-Polyethylene Glycol-Dibenzocyclooctyne A Linear Heterobifunctional Pegylation Reagent
__dirname
钟表刻度线
微信小程序自定义swiper轮播图面板指示点|小圆点|进度条
ThunderBirde无法登录问题、pycharm调试一直收集数据、RuntimeError: CUDA error: device-side assert triggered等疑难杂症解决
Error in render: “TypeError: Cannot read properties of null (reading ‘0‘)“ 报错解决方案