首页 新闻 会员 周边

typescript: 遭遇问题 "Unsafe assignment of an `any` value @typescript-eslint/no-unsafe-assignment"

0
悬赏园豆:30 [已解决问题] 解决于 2023-10-15 23:18

下面的 ts 代码

const userInfo = await UserService.getUserInfoWithToken(token, isPat)
const { spaceUserID, displayName } = userInfo

const session = <AuthenticationSession>{
    account: {
        id: spaceUserID.toString(),
        label: displayName,
    },
    id: `${this.providerId}-${userInfo.spaceUserID}`,
    accessToken: token,
    scopes: this.ensureScopes(null),
}

在执行 eslint 时报下面2个错误

Unsafe assignment of an `any` value  @typescript-eslint/no-unsafe-assignment
Unsafe call of an `any` typed value  @typescript-eslint/no-unsafe-call

请问如何解决?

问题补充:

getUserInfoWithToken 方法的主要实现代码

export async function getUserInfoWithToken(token: string, isPat: boolean): Promise<UserInfo> { 
    const req = await fetch(url, {
        headers: {
            authorization: `Bearer ${token}`,
            'content-type': 'application/json',
            'authorization-type': isPat ? 'pat' : '',
        },
    }) 
    return (await req.json()) as UserInfo
}

UserInfo 接口的实现代码

export interface UserInfo {
    userId: string
    spaceUserID: number
    displayName: string
}
dudu的主页 dudu | 高人七级 | 园豆:30948
提问于:2023-10-15 22:57
< >
分享
最佳答案
0

id: spaceUserID.toString() 改为 id: new Number(spaceUserID).toString() 解决了

dudu | 高人七级 |园豆:30948 | 2023-10-15 23:18
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册