Ts data type check error Type 'void' is not assignable to type' never []'.

recently started using typescript. But the damn code that did not report an error in JavaScript will report an error in typescript.
this type of check really does not know how to change it.
online and other great answers

data() {
    return {
      list: []   //
    };
},
async getList() {
  this.list = await Tanks.get();
},

clipboard.png

clipboard.png

Promise

clipboard.png

Mar.18,2022

A year later, I probably know what the reason is:

data() {
    return {
      list: []   //
    };
},

here, ts initializes an empty array of never [] type by default, which does not match the value type obtained by await Tanks.get (). It should be like this

.
data() {
    return {
      list: [] as any[]   // as Tanks.get()
    };
},

Please paste up the code. What type of this.list is it? How is the type returned by task.get declared?

generally speaking, async returns a promise, so you need to declare a Promise generic, but the return of resolve and reject may be different, you are more likely to be a joint generic, such as Promise < Object | string >, or directly any.

you provide too little information.

but I can give you an one-size-fits-all trick: ignore, is very discouraged.

/ / @ ts-ignore


if you use typescript, it is best to provide all kinds of type declarations, otherwise the type automatically inferred by the type of ts is probably not the one you want.

in addition, a mix of ts and js is highly deprecated unless you are refactoring a project.

Menu