Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to define an array of objects

Using vue 3, typescript and eslint

I have the following code:

...
data() {
  return {
    blah: '',
    blah2: []
    ...

and in the methods object I have:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

methods: {
  addEntry() :void {
    this.blah2.push({s:'whatever',b:false});
  }
  ...

I’m getting this error:

Argument of type '{ s: string; b: boolean; }' is not assignable to parameter of type 'never'

I’ve tried the following, none of which work:

blah2: any[] = []

blah2: Array<{ s: string, b: Boolean }> = Array()

blah2: Array<{ s: string, b: Boolean }>[] = []

blah2: { s: string, b: Boolean } = []

blah2: { s: string, b: Boolean }[] = []

blah2: any[{ s: string, b: Boolean }] = []

What is the correct way to do this?

Note: If I hardcode some initial data like so:

...
data() {
  return {
    blah: '',
    blah2: [
      {s:'whatever',b:false}
    ]
    ...

then it has no problem, so, I guess, it is working the declaration out from the data.

>Solution :

You need to set the return type of the function
See example here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading