首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >@typescript-eslint/no-不安全-赋值:“任意”值的不安全分配

@typescript-eslint/no-不安全-赋值:“任意”值的不安全分配
EN

Stack Overflow用户
提问于 2022-02-23 07:27:32
回答 1查看 4.7K关注 0票数 0

我在客户端使用TypeScript,当我运行应用程序时,有两个错误显示如下:

代码语言:javascript
运行
复制
@typescript-eslint/no-unsafe-assignment: Unsafe assignment of an `any` value.

@typescript-eslint/no-unsafe-member-access: Unsafe member access .value on an `any` value.

这是我的代码:

代码语言:javascript
运行
复制
const userInfo = ref({} as UserInfo) // first error

$f.axios
    .get<UserInfo>('/comm/auth/get-my-info')
    .then((result) => {
      userInfo.value = result.data // second error
    })
    .catch((err) => {
      //....
    })

UserInfo in .ts:

代码语言:javascript
运行
复制
export interface UserInfo {
  userName: string
  realName: string
  password: string | null
  email: string | null
  mobilePhone: string | null
  appToken: string | null
  internalTags: string | null
  enabledState: boolean
  isApiUser: boolean
  createDt: Date
  createP: string
  createPn: string | null
  updateDt: Date
  updateP: string
  updatePn: string | null
  firstLogin: true
  passwordExpiresDt: Date | null
  rolesString: string | null
  userRoleIds: number[] | null
}
EN

回答 1

Stack Overflow用户

发布于 2022-02-23 07:48:18

@typescript-eslint/no-不安全-赋值:any值的不安全分配。

这意味着response.data中的属性可能与接口UserInfo的属性不匹配。

我们可以通过添加类似于ESLint warning的来摆脱这个。

代码语言:javascript
运行
复制
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment

@typescript-eslint/no-不安全-成员访问:any值上的不安全成员访问.value。

根据上面的错误,您的value接口中没有任何UserInfo属性。因此,它期望value属性应该在接口中。通过将其添加到UserInfo中,问题将得到解决。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71232812

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档