Home > Enterprise >  How to solve TS2532 Object is possibly 'undefined'. with an Array?
How to solve TS2532 Object is possibly 'undefined'. with an Array?

Time:09-23

I have:

interface CompanyInfoProps {
    data: Company;
}

Where:

declare interface Company {
    /// some stuff
    id: string;
    companyAdmins?: CompanyAdmin[];
}

I am trying to do:

{data.companyAdmins[0].user.firstName}

But I get the error:

Object is possibly 'undefined'.  TS2532

I tried {data.companyAdmins?.length > 0 && data!.companyAdmins[0]!.user?.firstName} but I get the same error. What am I doing wrong?

CodePudding user response:

You should ashure typescript that companyAdmins is defined

  • Related