msal打字稿错误错误'void |类型不存在属性'accessToken' TokenResponse'

时间:2020-05-25 12:04:14

标签: javascript typescript

以下代码为response.accessToken生成以下TypeScript错误:

TS2339:类型'void |上不存在属性'accessToken'。令牌响应”。 enter image description here

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;
};

我在做什么错了?

1 个答案:

答案 0 :(得分:1)

尽管您使用的是Error: Expected a value of type 'FirebaseError', but got one of type 'DomException' at Object.throw_ [as throw] (http://localhost:44987/dart_sdk.js:4461:11) at Object.castError (http://localhost:44987/dart_sdk.js:4432:15) at Object.cast [as as] (http://localhost:44987/dart_sdk.js:4748:17) at dart.AnonymousJSType.new.as (http://localhost:44987/dart_sdk.js:6186:64) at handleThenable (http://localhost:44987/packages/firebase/src/storage.dart.lib.js:3264:96) at handleThenable.throw (<anonymous>) at http://localhost:44987/dart_sdk.js:36909:38 at _RootZone.runBinary (http://localhost:44987/dart_sdk.js:36762:58) at _FutureListener.thenAwait.handleError (http://localhost:44987/dart_sdk.js:31933:48) at handleError (http://localhost:44987/dart_sdk.js:32475:51) at Function._propagateToListeners (http://localhost:44987/dart_sdk.js:32498:17) at _Future.new.[_completeError] (http://localhost:44987/dart_sdk.js:32354:23) at async._AsyncCallbackEntry.new.callback (http://localhost:44987/dart_sdk.js:32392:31) at Object._microtaskLoop (http://localhost:44987/dart_sdk.js:37015:13) at _startMicrotaskLoop (http://localhost:44987/dart_sdk.js:37021:13) ,但请注意您的catch。我会这样写这段代码:

await

现在,为什么在import * as Msal from '@azure/msal-browser' export async function getTokenPopup(request: Msal.TokenExchangeParameters) { try { return await auth.acquireTokenSilent(request); } catch (error) { return await auth.acquireTokenPopup(request); } } const getGraphDetails = async ( uri: string, scopes: Msal.TokenExchangeParameters, axiosConfig?: AxiosRequestConfig ) => { try { const response = await getTokenPopup(scopes); return callGraph(uri, response.accessToken, axiosConfig); } catch (error) { throw new Error("You could not get a token"); } } 中得到void。函数response对于getTokenPopupacquireTokenSilent都可能失败。因此,函数acquireTokenPopup将引发错误(或不返回任何内容,具体取决于您的实现)。

TypeScript看到了这一点,并添加了getTokenPopup类型,以表明有可能无法获得响应。