当前位置:网站首页>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 string
Use 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 passed
Or use TypeScript's as keyword for type assertion
return ` - ${ route.name as string } Page`; // TypeScript validation passed
边栏推荐
猜你喜欢
随机推荐
JS对象, 函数和作用域
js eventLoop 事件循环机制
[campo/random-user-agent]随机伪造你的User-Agent
TCP通信程序
MySql高级 -- 约束
PHP有哪些框架?
SQL: DDL, DML, DQL, DCL corresponding introduction and demonstration
Thread Pool (Introduction and Use of Thread Pool)
宝塔邮局邮箱设置成功后能发送不能接收问题处理
微信小程序开发视频加载:[渲染层网络层错误] Failed to load media
js 中this指向
vim编辑模式
PHP 给图片添加全图水印
微信小程序全局组件的定义
easyswoole 使用redis执行geoRadiusByMember Count无效修复
ES6迭代器解释举例
每日五道面试题总结 22/7/26
page load process
14.JS语句和注释,变量和数据类型
display,visibility,opacity