报错如下:
Type ‘string | null’ is not assignable to type ‘string’.
Type ‘null’ is not assignable to type ‘string’.ts(2322)
解决办法:
token: localStorage.getItem("token") || ''
Type ‘string | null‘ is not assignable to type ‘string‘. Type ‘null‘ is not assignable to type ‘
报错如下:Type ‘string | null’ is not assignable to type ‘string’.Type ‘null’ is not assignable to type ‘string’.ts(2322)解决办法:token: localStorage.getItem("token") || ''
使用localStorage将Vue3中data声明的一个数组进行解析时。由于定义如下:
notFinish: JSON.parse(localStorage.getItem('notFinish')),
出现了如下错误:
Type '
null' is not
assignable to
type '
string'
解决方法:
可以“或”一个空数组字符串,即改成如下形式:
notFinish: JSON.parse(localStorage.getItem('notFinish')) || '[]',
在我用react + typescript写项目的时候,ts总会报一些类型的错误,比如下图的错误:Type ‘string | string[]’ is not assignable to type ‘string | undefined’.
在我确定我的类型是正确的情况下不理解为啥报这样一个错,在网上找了很久,就是加类型断言,解决方法如下图:(值 as 类型)
这样就不会报错拉。
关于typescript类型断言,整理知识点如下:
typescript很强大,但是用不好时也会很头痛,开发遇到类型错误的
let eleCanvas: HTMLCanvasElement;
const contentCavans = document.getElementById("content");
if(contentCavans) {
eleCanvas = contentCavans
第三种:类型断言
每次yarn build项目的时候就会报这个错误。刚开始找了很久原因,终于把总是解决了
总是分析,这种情况是因为vue3版本在多个v-for嵌套循环的时候,因为key的指引值为空,直接把第两个for的index 去掉,改为id或其他值即可。
<div v-for="(item,index) in oneArr" :key="index">
<span v-for="(itemSon,indexSon) in item.sonArr" :key="indexSon"&g
@HostListener(‘mouseleave’) onMouseLeave(): any {
this.highlight(null); // 报错null 改为’ ’
null 改为’’ 即可
‘App‘ cannot be used as a JSX component. Its return type ‘Element[]‘ is not a valid JSX element.
Type ‘string | null‘ is not assignable to type ‘string‘. Type ‘null‘ is not assignable to type ‘