当前位置:网站首页>TypeScript error error TS2469, error TS2731 solution
TypeScript error error TS2469, error TS2731 solution
2022-08-02 03:53:00 【varBlue】
Foreword
When creating a Vue3 + TypeScript + vueRouter project, when using route.name in a computed property to get the current page name, error TS2469 and error TS2731 appear in tsc validation
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 expressionin 'String(...)'.On the Internet, I found that there is currently no relevant solution (20220728). After investigation, it is determined that the error may be caused by the type of route.name being inferred as symbol when TypeScript performs type checking. To view the solution, you can directly jump to "Solution"
Error conditions
1. Error source
In the Vue combined SFC writing, use the computed() function to set the computed properties and concatenate them into the required string style
In this case, TypeScript performs type checking and an error TS2469 occurs
src/components/xx.vue(xx,xx): error TS2469: The '+' operator cannot be applied to type 'symbol'.If the return part of the computed property is written in the following style
return ` - ${ route.name } Page`;TypeScript performs type checking with 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 expressionin 'String(...)'.2. Reason search
Use the typeof() function to read the type of route.name and return a string
console.log(typeof(route.name)); // return stringUse TypeScript Playground to view error conditionsAnd type inference, the result is as follows


The symbol type is a basic data type added to ES6 to avoid overriding properties or methods defined by third-party JavaScript plugins and frameworks
Combining the error TS2469 and error TS2731 error prompts, it is judged that the cause of the error is that TypeScript judges the type of route.name as symbol
The symbol type cannot be concatenated with other strings, so this error is raised
Solution
Convert the route.name inferred as symbol type by TypeScript to string type
Type conversion using JavaScript's String() function
return ' - ' + String(route.name) + ' Page'; // TypeScript validation passedOr use TypeScript's as keyword for type assertion
return ` - ${ route.name as string } Page`; // TypeScript validation passed边栏推荐
猜你喜欢

1.13 学习JS

MySql高级 -- 约束

解决MySQL创建子视图并查看的时候,字符集报错问题

New usage of string variable parsing in PHP8.2

第一次手撕代码,如何解出全排列问题

5.20今日学习

SQL:DDL、DML、DQL、DCL相应介绍以及演示

你的本地创建的项目库还在手动创建远端代码仓库再推送吗,该用它了

Living to detect the Adaptive Normalized Representation Learning for GeneralizableFace Anti - Spoofing reading notes

PHP有哪些杀手级超厉害框架或库或应用?
随机推荐
解决uni-app 打包H5网站 下载图片问题
(2)Thinkphp6模板引擎**标签
我的小笔记 =》原生微信小程序
js 之 Object.defineProperty()
微信小程序九宫格抽奖和转盘抽奖的实现
PHP基金会三月新闻公告发布
PHP图片压缩到指定的大小
URL module
14.JS语句和注释,变量和数据类型
ES6迭代器解释举例
正则笔记(2)- 正则表达式位置匹配攻略
PHP有哪些框架?
Using PHPMailer send mail
js scope and closure
5.20今日学习
clock tick marks
多线程(实现多线程、线程同步、生产者消费者)
13.JS输出内容和语法
PHP8.2 version release administrator and release plan
数组的高级操作