0

I'm trying to solve compiler error " Object is possibly 'undefined' "

const destinationColumnIndex = (): number => {
  if (typeof result.destination === 'undefined') {
    return 0;
  }
  return boardData.findIndex(
    (column) => column.id === Number(result.destination.droppableId)
  );
};

but typescript compiler still tells me that "result.destination" may be undefined.

I have tried also:

  if (result.destination === undefined) {
    return 0;
  }

and:

  if (!result.destination) {
    return 0;
  }

and:

   if (!result || typeof result.destination === 'undefined') {
    return 0;
  }

and nothing works. Even thought it may be some bug so i restarted VS Code but there are still the same error.

EDIT - MORE CODE:

  const onDragEnd = async (result: DropResult) => {
if (!result.destination) {
  return;
}

const sourceColumnIndex = (): number =>
  boardData.findIndex(
    (column) => column.id === Number(result.source.droppableId)
  );

const destinationColumnIndex = (): number => {
  if (typeof result === 'undefined' || result.destination === undefined) {
    return 0;
  }
  return boardData.findIndex(
    (column) => column.id === Number(result.destination.droppableId)
  );
};

it's function inside of react component

4
  • 1
    does it say which object? Commented Mar 31, 2021 at 14:56
  • it underlines 'result.destiantion' Commented Mar 31, 2021 at 14:59
  • if(result?.destination === 'undefined') Commented Mar 31, 2021 at 15:00
  • it say "This condition will always return 'false' since the types 'DraggableLocation | undefined' and 'string' have no overlap." When i do if(typeof result?.destination === 'undefined') same error as before. Commented Mar 31, 2021 at 15:06

1 Answer 1

1

You should just do:

  if (result === undefined || result?.destination === undefined) {
    return 0;
  }

Checking typeof is not a good way to check for undefined.

or

  if (!result || result?.destination === undefined) {
    return 0;
  }

UPDATE

try this:

const onDragEnd = (result: DropResult) => {
  if (!result || !result.destination) {
    return;
  }

  const sourceColumnIndex = (): number =>
    boardData.findIndex(
      (column) => column.id === Number(result.source?.droppableId)
    );

  const destinationColumnIndex = (): number => {
    if (!result || !result.destination) {
      return 0;
    }
    return boardData.findIndex(
      (column) => column.id === Number(result.destination?.droppableId)
    );
  };
}
Sign up to request clarification or add additional context in comments.

7 Comments

curious, why is using typeof not good for checking undefined?
unfortunately none of these work for me. There is still an error.
try changing the first to result?.destination. I edited in my answer
because checking for the value itself is basically the same thing but more straight forward.
where is the variable result declared? can you post the full thing?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.