【发布时间】:2020-05-25 20:17:57
【问题描述】:
下面的代码为 response.accessToken 生成以下 TypeScript 错误:
import * as Msal from '@azure/msal-browser'
export async function getTokenPopup(request: Msal.TokenExchangeParameters) {
return await auth.acquireTokenSilent(request).catch(async () => {
return await auth.acquireTokenPopup(request).catch((error) => {
console.log('login with popup failed: ', error)
})
})
}
const getGraphDetails = async (
uri: string,
scopes: Msal.TokenExchangeParameters,
axiosConfig?: AxiosRequestConfig
) => {
return getTokenPopup(scopes).then((response) => {
return callGraph(uri, response.accessToken, axiosConfig)
})
}
在检查 TokenResponse 的 TS definition 时,它清楚地指出属性 accessToken 在对象上可用:
export type TokenResponse = AuthResponse & {
uniqueId: string;
tenantId: string;
scopes: Array<string>;
tokenType: string;
idToken: string;
idTokenClaims: StringDict;
accessToken: string;
refreshToken: string;
expiresOn: Date;
account: Account;
};
我做错了什么?
【问题讨论】:
-
请注意,为什么不使用 try/catch 和 await 呢? TypeScript 可能会看到您的
getTokenPopup返回 void。如果两个 Promise 都失败,则返回 void。 -
此代码是示例的副本。我只是希望它也可以与 TS 一起使用。
标签: javascript typescript