前言

在 Vue3 + TypeScript + pinia 项目中,使用 get() set() 定义计算属性时,出现 '不能将类型“WritableComputedRef<string>”分配给类型“string” ts(2322)' 或 "error TS2322: Type 'WritableComputedRef<string>' is not assignable to type 'string'" 错误

src/components/xxxx.vue(xx,xx): error TS2322: Type 'WritableComputedRef<string>' is not assignable to type 'string'.

错误复现

1. xxxx.vue 部分

<template><input v-model="textvalue" type="text" />
</template><script setup lang="ts">import { computed } from 'vue'// piniaimport { useHelloworldStore } from '../stores/helloworldStore'const store = useHelloworldStore()// 报错:不能将类型“WritableComputedRef<string>”分配给类型“string” ts(2322)const textvalue: string = computed({get() {return store.textvalue},set(newValue: string) {return store.changeTextvalue(newValue)}})
</script>

2. helloworldStore.ts 部分

import { defineStore } from 'pinia'export const useHelloworldStore = defineStore('helloworldStore', {state: () => {return {textvalue: '',}},actions: {changeTextvalue(newVal: string) {setTimeout(() => {this.textvalue = newVal}, 300)},}
})

3. 错误原因

vue3 的 computed 函数返回的是一个 ref 类型的响应式对象,此处传入 string 类型的 get() 和 set() 后,computed 函数返回的是 WritableComputedRef<string> 类型的值,而非 string 类型

但本文在 textvalue 计算属性处声明的类型是 string,与返回类型 WritableComputedRef<string> 不符,所以会报错:不能将类型“WritableComputedRef<string>”分配给类型“string” ts(2322)

解决方案

方法一:删掉 textvalue 处的类型声明

利用 vue3 的 <script setup> 语法糖,vue3 会自动推断 textvalue 的类型为 WritableComputedRef<string> ,解决问题

  // 验证通过const textvalue = computed({get() {return store.textvalue},set(newValue: string) {return store.changeTextvalue(newValue)}})

或使用 export default difineComponent({...}) 与 setup() 函数,同样会自动推断类型

方法二:声明 textvalue 为 WritableComputedRef<type> 类

从 vue 中引入 WritableComputedRef 类,将 textvalue 类型声明为 WritableComputedRef<type>,解决问题,适用于需要声明计算属性类型的场景

  import { computed, type WritableComputedRef } from 'vue'// 验证通过const textvalue: WritableComputedRef<string> = computed({get() {return store.textvalue},set(newValue: string) {return store.changeTextvalue(newValue)}})

error TS2322: Type ‘WritableComputedRef<string>‘ is not assignable to type ‘string‘ 解决方法相关推荐

  1. Type ‘HTMLElement | null‘ is not assignable to type ‘HTMLElement‘

    在typescript3.9中,以下代码编译时会提示错误: const elem : HTMLElement = document.getElementById('someid'); // Type ...

  2. Mysql添加用户错误:ERROR 1364 (HY000): Field ‘ssl_cipher‘ doesn‘t have a default value解决方法

    Mysql添加用户错误:ERROR 1364 (HY000): Field 'ssl_cipher' doesn't have a default value解决方法 参考文章: (1)Mysql添加 ...

  3. Json string value cannot have line breaks(解决方法)

    Json string value cannot have line breaks(解决方法) 参考文章: (1)Json string value cannot have line breaks(解 ...

  4. ERROR: Could not find a version that satisfies the requirement XXX解决方法

    出现问题:ERROR: Could not find a version that satisfies the requirement XXX解决方法 找了很久终于找到能解决这种报错的方法了. 直接选 ...

  5. Python pip install 安装包报错ERROR: Could not find a version that satisfies the requirement XXX解决方法

    Python pip install 安装包报错ERROR: Could not find a version that satisfies the requirement XXX解决方法 文章目录 ...

  6. 虚幻引擎UE4.25版本使用Android打包出错提示:ERROR: cmd.exe failed with args /c 和 :app:assembleDebug的解决方法

    虚幻引擎 UE4.25版本使用Android打包出错提示:ERROR: cmd.exe failed with args /c 和 :app:assembleDebug的解决方法: 在虚幻引擎的jdk ...

  7. Keil 5下出现Error: L6218E: Undefined symbol Delay(unsigned) (referred from main.o).的解决方法

    问题背景: 今天在给stm32f407vgt6点灯时,想实现循环闪烁,于是想从以前的工程中移植一个delay延时函数,移植后编译时发现出现错误: Error: L6218E: Undefined sy ...

  8. 记一次SpringBoot操作redis报错 Error creating bean with name ‘dataSource‘ defined in class path resource解决方法

    代码块 以下为Springboot操作Redis一些简单的测试代码 maven依赖 <dependency><groupId>org.springframework.boot& ...

  9. 对于在git上面拉代码报“error: RPC failed; curl 56 OpenSSL SSL_read: SSL_ERROR_SYSCALL, errno 10054“解决方法

    对于在git上面拉代码报"error: RPC failed; curl 56 OpenSSL SSL_read: SSL_ERROR_SYSCALL, errno 10054"解 ...

  10. error 1044 (42000):access denied for user ''@'localhost' to database 'mysql' 解决方法

    重新安装mariaDB数据库时,出现安装权限问题,中止安装发现居然能够登陆mysql,但是登陆时可以不填密码,操作数据库时会报错:ERROR 1044 (42000): Access denied f ...

最新文章

  1. Vim 中文件目录浏览插件——NERD tree
  2. Sentinel介绍与使用
  3. 【渝粤题库】陕西师范大学200831 编译原理 作业
  4. mysql error 1130 hy000:Host ‘localhost‘ is not allowed to connect to this mysql server 解决方案
  5. sql 查询手动创建的表_学习SQL:使用SQL查询手动创建报告
  6. 使用jquery获取ul中当前正在点击的li的索引
  7. python数据分析特训营课件,Python数据分析PPT学习课件
  8. SX1278设计(硬件部分)
  9. 【戒焦戒躁,can win】Linux--inode
  10. 一般总账科目 调整 为统驭科目时报错:只以本位币计的统驭科目余额未定义
  11. android 点击提示音,Android获取手机默认模式/提示音/响铃
  12. 基于ESP8266与51单片机的震动报警器(车辆报警器)(简单向)
  13. 第一部分_SQL查询语句学习
  14. Python的一些读书笔记
  15. suse linux VNC配置
  16. 小孙讲认证:ASTM F963 -17 CPSIA CPC 适用产品:婴儿磨牙产品、牙胶
  17. Py_Finalize引发的异常
  18. 统一安全管理平台解决方案
  19. 应用覆盖安装后桌面图标消失问题
  20. CentOS8提高篇8:CentOS制作U盘启动盘

热门文章

  1. 高通qspr是哪几个单词得缩写_PIR是哪几个单词的缩写
  2. python十大语言领域_2017年十大编程语言,Python最流行,JAVA老化,GO语言野蛮增长!...
  3. 2022全球开发者薪资曝光:中国排第19名,平均年薪23,790美元
  4. 图形程序设计--变形曲线绘制C++代码
  5. linux创建用户、用户组、授权读写权限等权限相关常用命令
  6. c语言烧烤机编程,王霄鹏:炮哥的c位烧烤如何练就而成?
  7. 巨头包围下,“社交+”模式APP如何打造“增长回路”?
  8. python format和百分号的区别_python 格式化输出百分号的方法
  9. 当着整个商场的面,iPhone电池就这么炸了
  10. 电容笔有什么用?电容笔十大品牌排行