2

I have tried many different formats, but can only make this work:

// This works
<script setup lang="ts">
import { reactive } from 'vue'
import { IPixabayItem } from '../interfaces/IPixapayItem'
const imageList: IPixabayItem[] = []
const foundImages = reactive(
    {
        images: imageList
    }
)
</script>

Somehow I would like to avoid the const 'imageList' and instantiate the 'IPixabayItem[]' inside the reactive object; but cannot make this transpile.

// This dosn't work
<script setup lang="ts">
import { reactive } from 'vue'
import { IPixabayItem } from '../interfaces/IPixapayItem'
const foundImages = reactive(
    {
        images: IPixabayItem[] = []
    }
)
</script>

Help appreciated

1 Answer 1

2

You can provide an empty array as the value for images and set IPixabayItem[] as the generic parameter of reactive.

const foundImages = reactive<{images: IPixabayItem[]}>({
    images: []
})

Playground


or you could use the as keyword:

const foundImages = reactive({
    images: [] as IPixabayItem[]
})

Playground

2
  • Wow, Thanks Tobis S. Your idea seems to work !! ..hmm Sorry my formating. I'm newbee in making posts in stackocerflow :-( <script setup lang="ts"> import { reactive } from 'vue' import { IPixabayItem } from '../interfaces/IPixapayItem' const foundImages = reactive( { images: [] as IPixabayItem[] } ) </script>
    – erm2000
    Commented May 30, 2022 at 22:43
  • Hi Tobias Sorry my misspelling of Tobias (and stackoverflow) but you deserve a "tumps up" for your great reply; which I cannot give :-( Stackoverflow "Tumps up" reply: "Thanks for the feedback! You need at least 15 reputation to cast a vote, but your feedback has been recorded." /erm2000
    – erm2000
    Commented May 30, 2022 at 23:11

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.