Perplexityy
Perplexityy

Reputation: 569

Property 'push' does not exist on type number

I have an array, and I want it to store types of:

[[number, number], number[]]

But when I shift() elements from it, I get the error in the title. Why does TypeScript think my path variable is a number, and not an array of numbers?

export const myFunction = (coordinates: [number, number]): [[number, number]] => {
    const [i, j] = coordinates;
    const queue: [[number, number], number[]] = [[i, j], []]
    const [results, path] = queue.shift();
    path.push(results[0]);
    return path
}

Upvotes: 0

Views: 466

Answers (1)

Jeroenouw
Jeroenouw

Reputation: 157

By using queue.shift();, you remove the first element of the queue array. This element is [i, j], then you basically say results = i and path = j. Example below:

export const myFunction = (coordinates: [number, number]): [[number, number]] => {
    const [i, j] = coordinates;
    const queue: [[number, number], number[]] = [[i, j], []]
    // get the first and second element of the array;
    const [results, path] = queue;
    // then remove coordinates;
    queue.shift(); 
    path.push(results[0]);
    return path
}

Upvotes: 1

Related Questions