typescript类型的问题?-灵析社区

劳资最帅

我想解决的是res.Code下面的红线问题 ![image.png](https://wmprod.oss-cn-shanghai.aliyuncs.com/c/user/20241012/91f3f4a393142e0c55529ce5a38d2bcd.png) 下面是loginApi接口的类型定义: export const loginApi = (data: UserType): Promise> => { return request.post({ url: '/BackLogin/Login', data }) } 其中UserType类型的定义: export type UserType = { userNo: string password: string role: string roleId: string permissions: string | string[] } IResponse类型的定义: declare interface IResponse { Code: string|int Data: T extends any ? T : T & any Msg:string|any } 为什么Promise的返回值Code,Data,Msg下面都带红线? ![image.png](https://wmprod.oss-cn-shanghai.aliyuncs.com/c/user/20241012/967d85981b2d8087840ffa8013f06bdc.png) 代码: const res = await loginApi(data).catch(e=>{ loading.value=false }) if (res.Code===0) { wsCache.set(appStore.getUserInfo, res.Data) // 是否使用动态路由 if (appStore.getDynamicRouter) { console.log("动态路由") } else { } }else{ ElMessage.error(res.Msg) }

阅读量:154

点赞量:0

问AI
const res = await loginApi(data).catch(e=>{ loading.value=false }) as IResponse; if (res && res.Code===0) { wsCache.set(appStore.getUserInfo, res.Data) // 是否使用动态路由 if (appStore.getDynamicRouter) { console.log("动态路由") } else { } }else{ ElMessage.error(res.Msg) } 但看loginApi的类型声明,是没什么问题的,建议为"request.post"添加类型声明。 export const loginApi = (data: UserType): Promise> => { return request.post({ url: '/BackLogin/Login', data }); }