Arrays Array.shift()在一段时间内(Array.length)在Typescript中给出错误

Arrays Array.shift()在一段时间内(Array.length)在Typescript中给出错误,arrays,typescript,Arrays,Typescript,我目前正在typescript中实现一个*并使用一个数组作为队列,只要队列中有项,我的while循环就会继续,但typescript不理解循环数组中的这一点。shift()不能返回未定义的 interface edge { weight:number; vertex:Vertex; } interface Vertex { name:string; successor:edge[]; heuristic:number; dist:number;

我目前正在typescript中实现一个*并使用一个数组作为队列,只要队列中有项,我的while循环就会继续,但typescript不理解循环数组中的这一点。shift()不能返回未定义的

interface edge {
    weight:number;
    vertex:Vertex;
}

interface Vertex {
    name:string;
    successor:edge[];
    heuristic:number;
    dist:number;
    parent?:Vertex;
    visited:boolean;
}

function Astar(startingNode:Vertex, goal:Vertex, heuristicFunc?:(node:Vertex, goal:Vertex) => number):Vertex[]|null {
    startingNode.dist = 0;
    const queue = [startingNode];
    while(queue.length) {
        queue.sort((a, b) => a.dist + a.heuristic - b.dist - b.heuristic);
        let currentNode = queue.shift();
        if(currentNode == goal) {
            const path:Vertex[] = []
            while(currentNode) {
                path.unshift(currentNode);
                currentNode = currentNode.parent;
            }
            return path;
        }
        currentNode.visited = true;
        currentNode.successor.forEach(({weight, vertex}) => {
            if(currentNode.dist + weight < vertex.dist) {
                vertex.dist = currentNode.dist + weight;
                vertex.parent = currentNode;
                vertex.visited = false;
            }
        })
        currentNode.successor.filter(({vertex})=> !vertex.visited && !queue.includes(vertex)).forEach(({vertex}) => {
            if(heuristicFunc) vertex.heuristic = heuristicFunc(vertex, goal);
            queue.push(vertex);
        })
    }
    return null;
}

我真的需要隐式检查它是否未定义吗?或者还有其他选项吗?

是的,这就是键入
shift
的方式。但通常,有一个更好的选择-这取决于您在循环中的代码到底要完成什么。@CertainPerformance如果您想看一看合理的话,我删除了所有不必要的a*代码行并上传了它。您可能应该添加一个
shift
调用的末尾,以指示该值不会未定义。我经常看到人们在操纵数据结构时不必要地使用mutator方法,但是对于像这样复杂的算法,
shift
可能是最清晰的方法。我想你可以将它改为:
while(true){/*sort*/let currentNode=queue.shift();if(!currentNode)返回null;
,如果您真的想返回,但是没有太大区别。使用
是非常有用的fine@CertainPerformance工作非常完美!只需更改一点路径重建循环,但现在所有错误都消失了!非常感谢
TSError: ⨯ Unable to compile TypeScript:
Astar.ts:29:9 - error TS2532: Object is possibly 'undefined'.

29         currentNode.visited = true;
           ~~~~~~~~~~~
Astar.ts:30:9 - error TS2532: Object is possibly 'undefined'.

30         currentNode.successor.forEach(({weight, vertex}) => {
           ~~~~~~~~~~~
Astar.ts:31:16 - error TS2532: Object is possibly 'undefined'.

31             if(currentNode.dist + weight < vertex.dist) {
                  ~~~~~~~~~~~
Astar.ts:32:31 - error TS2532: Object is possibly 'undefined'.

32                 vertex.dist = currentNode.dist + weight;
                                 ~~~~~~~~~~~
Astar.ts:37:9 - error TS2532: Object is possibly 'undefined'.

37         currentNode.successor.filter(({vertex})=> !vertex.visited && !queue.includes(vertex)).forEach(({vertex}) => {
{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "lib": ["es6", "dom", "es2017"],
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true
  }
}