【问题标题】:How do you tackle the following error in typescript你如何解决打字稿中的以下错误
【发布时间】:2021-08-31 17:18:48
【问题描述】:

我是 typescript 的新手并开始学习它,因为每个人似乎都非常坚持使用 typescript 编码而不是js。所以,试一试,我想我会先将ts 应用于简单的数据结构问题。但是我陷入了以下我似乎没有得到的东西。

interface GraphType {
  [index: string]: string[];
}

const graph: GraphType = {
  a: ['c', 'b'],
  b: ['d'],
  c: ['e'],
  d: ['f'],
  e: [],
  f: []
}

const depthFirstPrint = (graph: GraphType, source: string) => {
  const stack: string[] = [source];
  
  while(stack.length > 0) {
    const current: string = stack.pop(); // <1>
    console.log(current);
    for (let neighbor of graph[current]) { //<2>
      stack.push(neighbor);
    }
  }
};

depthFirstPrint(graph, 'a'); //abdfce

显示错误:

&lt;1&gt;Type 'string | undefined' is not assignable to type 'string'. Type 'undefined' is not assignable to type 'string'
这很令人困惑,因为我将stack 声明为string[],即array of strings,并且我正在检查数组stack 是否与while(stack.length &gt; 0) 不为空。

&lt;2&gt;Object is possibly 'undefined',我不确定如何处理这个问题。在js 中,如果它的undefinedfor 循环将不会执行,这是理想的行为。但我不知道如何在ts 中处理这个问题。

【问题讨论】:

  • @Joe 感谢您的回复。所以&lt;1&gt; 似乎是编译器的问题,我想我必须在stack.pop() 之后使用! 运算符,我觉得这很难看。您对&lt;2&gt; 需要做什么有答案或建议。顺便说一句,感谢该链接,它非常有帮助。
  • 您的 tsconfig 中是否有一些不寻常的设置?我尝试将您的代码粘贴到 TS Playground typescriptlang.org/play 中,但只收到错误 &lt;1&gt;,而不是错误 &lt;2&gt;
  • @pandubear 感谢您的回复。我不这么认为。我将target:ES2020module: commonjsmoduleResolution: nodestrict: trueadditionalChecks 下的所有内容设置为 true(我猜这可能是值得一看的)。 noPropertyAccessFromIndexSignature: true 现在引起了我的注意,因为你提到了它。是这样吗?我真的不知道它是做什么的,但我喜欢让我的 tsconfig 尽可能严格,所以所有这些都是true

标签: typescript


【解决方案1】:

是的,您已经在while 中检查了stack.length &gt; 0,但是编译器如何知道stack.pop() 总是有价值?

例如,假设stack.length1while(stack.length &gt; 0) 为真,但在下面的代码中current1current2 未定义,而stack.length 大于1

while(stack.length > 0) {
    const current = stack.pop(); 
    const current1 = stack.pop(); 
    const current2 = stack.pop(); 
}

解决方案

您可以将current 定义为string | undefined,但在使用该变量之前,您需要检查它是否具有价值:

const depthFirstPrint = (graph: GraphType, source: string) => {
  const stack: string[] = [source];
  
  while(stack.length > 0) {
    const current = stack.pop(); // <1>
    console.log(current);

    if(current)
      for (let neighbor of graph[current]) { //<2>
         stack.push(neighbor);
      }
  }
};

这是工作示例:PlaygroundLink

根据 OP 的评论更新:

如果你想在你的TSConfig 中打开noUncheckedIndexedAccess,你只需要更多检查:) 像这样:

while(stack.length > 0) {
    const current = stack.pop(); // <1>
    console.log(current);
    
    if(current) {
    let value = graph[current];
    
      if(value)
        for (let neighbor of value) { //<2>
           stack.push(neighbor);
        }
    }
  }

PlaygroundLink

【讨论】:

  • @Vaibhav_M 您的问题解决了吗?如果有问题请告诉我
  • 感谢您的回复。抱歉,我遇到了一些事情并被捆绑了。 &lt;2&gt; 的问题仍然存在,我得到 Object is possibly undefined。我检查了@pandubear 的建议,启用时引发该问题的是noUncheckedIndexedAccess。你也可以在操场上复制它。正如我告诉他的I don't really know what it does, but I like to have my tsconfig to as strict as possible, so made all of those to true。你觉得我应该怎么做。你知道那个选项有什么用吗?再次感谢。
  • @Vaibhav_M 你检查过我回答中的操场链接了吗?我看不到任何问题
  • 正如我所说,如果您在 Playground 的 TSConfig(在类型检查下)中打开 noUncheckedIndexedAccess,它会开始给出该错误(对于相同的代码)。我在我的项目的 TSConfig 中打开了它,所以我不知道在这种情况下该怎么做。感谢您的帮助。
  • 感谢您的回答。非常抱歉,我再次陷入困境,无法回来查看该网站。当然,我会接受答案,因为它回答了我所有的问题。再次感谢您为帮助我付出的所有时间和精力。
猜你喜欢
  • 1970-01-01
  • 2021-01-08
  • 1970-01-01
  • 2019-05-21
  • 2018-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多