0

I am converting an existing vue project (written in JS) to Typescript. I am mostly getting on well, but am having a problem with passing an array as props to a component. I am using Composition API / SFC.

For one component (which doesn't need an array), I have this structure:

<script setup lang="ts">
  import type { TrackTile } from '../../env.d';

  interface Props {
    tile: TrackTile,
    item: number
  }
  const props = defineProps<Props>()
  console.log('keyItem script setup');
  console.log(props.tile);

</script>

This transpiles without errors and then runs fine, so I assume my basic syntax is OK here. But another component mainGrid needs to be given an array of TrackCell types:

<script setup lang="ts">

  import type { TrackCell } from '../../env.d';
  interface Props {
    grid: TrackCell[][];
  }
  const props = defineProps<Props>()

  console.log('mainGrid script setup');
  console.log(props.grid[0][0]);
    
</script>

The component is called from its parent like this:

 <mainGrid :grid = trackGrid>
 </mainGrid>

In this case I get an error that props.grid[0] is undefined.

As a check to ensure that the trackGrid array (and its constituent trackCells) was defined and populated correctly, I temporarly moved the template for the mainGrid component into its parent, and everything then works as expected. So it appears that it's just the `defineProps' section which is incorrect.

trackGrid is reactive:

export const trackGrid: TrackCell[][] = reactive([] );

2 Answers 2

1

I don't think that's a typescript error, most likely the problem occurs in the log statement:

console.log(props.grid[0][0]);

Probably grid is an empty array after initialization?

Try

console.log(props.grid[0]?.[0]);

or remove the statement altogether


As a quick fix, you could initialize trackGrid with an empty row:

export const trackGrid: TrackCell[][] = reactive([[]]);
Sign up to request clarification or add additional context in comments.

3 Comments

I only added the log statement to show the problem more easily. Without it, the same undefined error occurs at the first use of one of the cells in the template itself. Forget to say that TrackGrid is reactive.
Well there you go, that's the same error, just at a different position then. If you add the code where you build the trackGrid and the template where the error occurs, it should be easy to identify
This morning I find that your original answer was partly correct! Too long to explain in a comment, so I'll add an answer which shows the sequence of events.
0

Turns out there wasn't any problem with the component props as posted! I added console.log(props.grid[0][0]); to solve a problem with the grid cells being undefined when they were used in the template. But that was a silly thing to do because of course the component's <setup> is called before the data has been fetched (in onBeforeMount).

After adding that erroneous debug line, I had corrected the actual error: I changed the declation of the array from

export const trackGrid: TrackCell[][] = reactive([...Array(8)].map(_=>Array(8)) );

to

export const trackGrid: TrackCell[][] = reactive([] );

but then I didn't remove the erroneous debug log!

Still not sure through if it should be

export const trackGrid: TrackCell[][] = reactive([] );

or

export const trackGrid: TrackCell[][] = reactive([[]] );

as both seem to work.

Anyway I hope that my original question may help someone in the future deal with arrays using TS with Vue.

2 Comments

I have a similar issue, but I want to have the elements within the array reactive as well and I don't seem to manage it. Do you have any ideas?
sorry, that was a while ago now, I have moved one. Maybe you could tag Moritz, he seems to be an expert on such things!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.