Home  >  Q&A  >  body text

Unallocated objects cannot be error checked in Typescript

I'm trying to convert the following js to typescript but I'm having trouble checking my account object because it's not allocated so the code won't build:

const accounts = state.msalInstance.getAllAccounts();
let account;

if (accounts.length) {
  account = accounts[0];
} else {
  await state.msalInstance
    .handleRedirectPromise()
    .then(redirectResponse => {
      if (redirectResponse !== null) {
        account = redirectResponse.account;
      } else {
        state.msalInstance.loginRedirect();
      }
    })
    .catch(error) => {
      console.error(`Error during authentication: ${error}`);
    });
}

if (account) {}

The code above worked fine in js so I've converted it to the following ts, but now the final if won't compile because I'm trying to use the account variable before assigning anything:

const accounts = state.msalInstance.getAllAccounts();
let account: object;

if (accounts.length) {
  account = accounts[0];
} else {
  await state.msalInstance
    .handleRedirectPromise()
    .then((redirectResponse: {
      account: object;
    }) => {
      if (redirectResponse !== null) {
        account = redirectResponse.account;
      } else {
        state.msalInstance.loginRedirect();
      }
    })
    .catch((error: {
      name: string;
    }) => {
      console.error(`Error during authentication: ${error}`);
    });
}

if (account) {
  // this won't compile anymore as account isn't assigned
}

How do I change the account object to see if it is empty or undefined? I tried setting it specifically to null but it says I can't set the object to null.

P粉986860950P粉986860950185 days ago407

reply all(2)I'll reply

  • P粉904450959

    P粉9044509592024-04-02 09:49:17

    The solution to the problem of not being able to use a variable until it is assigned a value is very simple - just assign it a value.

    You can make it clear that its value is initially undefined , which is what happens to a variable that is declared but not assigned a value. Or you can use a more intentional "null" initial value, such as null.

    Of course this will change your variable type to a union containing this initial value, but that's consistent with how you're using it, so I don't think it'll be an issue.

    By the way, object is not a very useful type. If you have linting, you may get a warning. Would some form of logging be more useful/clearer to you?

    reply
    0
  • P粉752290033

    P粉7522900332024-04-02 09:29:28

    When you do this ->

    let account: object;
    
    if (account) { // error here

    You tell Typescript that the account can only be an object , but Typescript will let you delay allocating it, but if it detects that there is a chance that the object will never be assigned to the account, it will give you - > The variable "account" is used before assignment.

    Consider you doing a real test -> if (account) {

    Logically your type is object or undefined

    let account: object | undefined;
    
    if (account) { // No error

    In some cases it might make more sense to explicitly unset the value, so some people also like to use null -> let account:object |null=null

    reply
    0
  • Cancelreply