打字稿null安全分解

时间:2019-01-02 04:47:20

标签: typescript

我输入了graphql查询,这些查询可以是深层次的对象,其中每个级别都可以为空,这使我的解构代码看起来很糟糕...

#include <iostream>
using namespace std;
int main() {
  int array[] = {10,  20, 30, 40, 50, 60, 70, 80, 90, 100, 200,
                 300, 400, 5006, 110, 550, 440, 330, 331, 41};

  // length of array is not provided

  int i, n, loc = -1;
  cout << "Enter value to find" << endl;
  cin >> n;
  for (i = 0; i < 5; i++) {
    if (array[i] == n) {
      loc = i;
    }
  }
  if (loc == -1)
    cout << "Number is not found in array" << endl;
  else
    cout << "the number is found at position " << loc << " and is " << n;
  return 0;
}

我可以通过提供null安全的解构函数来使它更干净吗...

const { levelOne } = data || {} as keyof typeof data;
conse { levelTwo } = levelOne || {} as keyof typeof levelOne;
...

还是有更好的方法进行内联销毁或安全销毁功能?

0 个答案:

没有答案