Javascript can';t检查typescript中是否未定义变量

Javascript can';t检查typescript中是否未定义变量,javascript,typescript,Javascript,Typescript,我正在尝试解决编译器错误“对象可能是‘未定义’” 但typescript编译器仍然告诉我“result.destination”可能未定义 我也尝试过: if (result.destination === undefined) { return 0; } 以及: 以及: 什么都不管用。甚至认为这可能是一些错误,所以我重新启动了VS代码,但仍然存在相同的错误 编辑-更多代码: const onDragEnd = async (result: DropResult) =>

我正在尝试解决编译器错误“对象可能是‘未定义’”

但typescript编译器仍然告诉我“result.destination”可能未定义

我也尝试过:

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

以及:

什么都不管用。甚至认为这可能是一些错误,所以我重新启动了VS代码,但仍然存在相同的错误

编辑-更多代码:

  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)
  );
};
它是react组件内部的功能

您只需执行以下操作:

  if (result === undefined || result?.destination === undefined) {
    return 0;
  }
检查
typeof
不是检查未定义类型的好方法

更新

试试这个:

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

它说的是哪个对象吗?它在'result.destinion'
if(result?.destination=='undefined')
下面加了下划线。它说“这个条件将总是返回'false',因为'DraggableLocation | undefined'类型和'string'类型没有重叠。”,为什么使用
typeof
不适合检查
未定义的
。仍有错误。请尝试将第一个更改为
result?。destination
。我在回答中进行了编辑,因为检查值本身基本上是一样的,但更直接。问号没有帮助
  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)
  );
};
  if (result === undefined || result?.destination === undefined) {
    return 0;
  }
  if (!result || result?.destination === undefined) {
    return 0;
  }
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)
    );
  };
}