Perplexityy
Perplexityy

Reputation: 569

Type number[] is not assignable to type number

I am trying to implement a depth first search function in TypeScript. I am trying to define the type of my stack, and I am clearly passing src, which is of type number[], to my stack. However, TypeScript is complaining that it is of type number. Why? How can I resolve this?

export const DFS = (rows: number, cols: number, src: number[], dest: number[], maze: boolean[][][]) => {
      const stack: [number[], number[], number[]] = [[src, [], [0, 0]]]
  }

Upvotes: 0

Views: 845

Answers (1)

satanTime
satanTime

Reputation: 13539

Looks like you put too many brackets.

export const DFS = (rows: number, cols: number, src: number[], dest: number[], maze: boolean[][][]) => {
  const stack: [number[], number[], number[]] = [src, [], [0, 0]]; // <- here in value.
}

if you want to have an array then you need to use Array<T> instead of the tuple.

export const DFS = (rows: number, cols: number, src: number[], dest: number[], maze: boolean[][][]) => {
  const stack: Array<[number[], number[], number[]]> = [[src, [], [0, 0]]]; // <- here in value.
}

Upvotes: 1

Related Questions