2
certificate?: any;

<S.Contents>{certificate[0]}</S.Contents>
<S.Contents>{certificate[1]}</S.Contents>
<S.Contents>{certificate[3]}</S.Contents>

If the type of props is set to any and used as an index of an array, it works well. But if you change that any type to Array, Generic type'Array' requires 1 type argument(s). I get an error like this.

When I print typeof to the console, the object comes out, so when I change it from Array to object, I get an error other. "Object is possibly 'undefined'."

setCertificate(res.data.qualification.certificate);

enter image description here

Since it is an original array, I tried to create elements by turning it into map, but it also didn't work with a map is not a function error. I don't know what to do with me. Help.

6
  • 1
    Can you try any[]? Or by seeing the example mentioned, it can be string[] Commented Mar 13, 2021 at 8:31
  • Even if you change it to any[] or string[], Object is possibly' undefined'. I am getting this error. Commented Mar 13, 2021 at 8:36
  • 2
    The one you might be getting in setCertificate. That is coming because you have marked certificates as optional. Either add a check before using the certificates or you can remove it as optional. TS here just makes sure you don't make any errors in the final code. So checks for whatever the possible issues that can happen. Commented Mar 13, 2021 at 8:38
  • So how do I change the code or settings? Sorry. I'm clumsy yet. Commented Mar 13, 2021 at 8:40
  • Can you update your question to show how the setCertificate is implemented? Commented Mar 13, 2021 at 8:46

2 Answers 2

2

You Always have to check for possible null or undefined values.

This is how I would do it to make it 100% safe

return (<div> {
    certificate && certificate.length > 0 ? (
      certificate.map((item, index) => ( 
      <S.Contents key = {index}> {item}</S.Contents>
      ))
    ) : null
  } </div>)

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much. Thanks for letting me know why. However, I tried the way you told me, "Type'{}' is missing the following properties from type'any[]': length, pop, push, concat, and 28 more." I get an error saying.
1

You get this error because you used an optional property for certificate. You need to add a check for undefined, to make sure it is actually defined. Assuming your react function looks something like this, this would be a fast way to solve your issue:

function ReactComponent(props: {
    certificate?: any[]
}) {
  const certificate = props.certificate || [];
  
  return (
        {certificate.map((certificateItem) => (
            <S.Contents>{certificateItem}</S.Contents>
        ))}
  );
}

This line const certificate = props.certifate || []; assigns the first value if it is not undefined to the variable certificate otherwise the second. An if statement would also work but would be more verbose in this case:

function ReactComponent(props: {
    certificate?: any[]
}) {
  let certificate = props.certificate;

  if(typeof certificate === "undefined") {
      certificate = [];
  }
  return  (
    <div>
        {certificate.map((certificateItem) => (
            <S.Contents>{certificateItem}</S.Contents>
        ))}
    </div>
  );
}

2 Comments

Thank you very much. Thanks for letting me know why. However, I tried the way you told me, "Type'{}' is missing the following properties from type'any[]': length, pop, push, concat, and 28 more." I get an error saying.
You seem to use an object instead of an array. {} is defining an empty object, [] is defining an empty array. Make sure you have initialized your variables with the correct type.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.