TypeScript提示Property ‘xxx’ does not exist on type ‘never’问题解决

IT 文章1周前更新 小编
0 0 0

在使用Vue3中使用TypeScript时,根据角标引用数组元素时出现如下错误:
TypeScript提示Property 'xxx' does not exist on type 'never'问题解决
[v_error]错误提示:Property ‘xxx’ does not exist on type ‘never'[/v_error]
其中stateformState定义代码如下:

let state = reactive({
	staplantypesArr: [],
	dynplantypesArr: [],
});
let formState = reactive({
	planType: undefined, 
});

而我报错代码部分就是想将经过筛选过滤后的得到的数组(赋值给了state.staplantypesArr数组了),然后再将state.staplantypesArr的第1个元素的planTypeCode属性赋值给formState.planType,结果就出现了如上图所见的错误,对于对于像潘老师这种专注后端,前端不咋地的人而言,不知道原因,但最终还是通过如下方案解决了:

// 计划类型
planTypes().then((arr: any) => {
	state.staplantypesArr = arr.filter((planType:any) => !planType.isDynamic);
	state.dynplantypesArr = arr.filter((planType:any) => planType.isDynamic);
	// 调用实现
	initSelectedPlanType();
});
// 新增一个方法,重点在于将赋值给重新定义的一个any类型的变量
const initSelectedPlanType = () => {
	let staArr:any = state.staplantypesArr;
	formState.planType = staArr[0].planTypeCode;
}

主要实现思路就是将 state.staplantypesArr赋值给重新定义的一个any类型的变量,然后再去获取数组元素调用属性赋值,以上就是TypeScript提示Property ‘xxx’ does not exist on type ‘never’问题解决方案,如果你有更好的解决方案,记得评论哦!

ad

程序员导航

优网导航旗下整合全网优质开发资源,一站式IT编程学习与工具大全网站

© 版权声明

相关文章

暂无评论

暂无评论...