Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TS Type is missing the following properties

I'm using Angular 7.x.x with TS in version 3.2.4.

I have two TS interfaces. One ist extending the other one:

This is the main interface:

export interface Result {
      var1: string;
      var2: number;
      var3: boolean;
} 

The second one just adds a property:

export interface ResultPlus extends Result {
      var4: boolean;
}

Now I have a service returning Observable<Result[]>.

In my component I subscribe on this service:

dataArray: ResultPlus[] = [];    

getResults(): void {
      this.service.getResults()
          .subscribe(data => {
            **this.dataArray** = (data as unknown as ResultPlus);
          });
     }

(There are no * in the code)

Now the this.dataArray (bold above - **) is underlined in red and it says:

error TS2740: Type 'ResultPlus' is missing the following properties from type 'ResultPlus[]': length, pop, push, concat, and 26 more.

What am I doing wrong?

Thank you in advance!

like image 591
A.Service Avatar asked Jan 28 '19 09:01

A.Service


People also ask

Is missing the following properties from type TypeScript react?

The React. js error "Type is missing the following properties from type" occurs when we don't pass any props to a component, or don't pass it all of the props it requires. To solve the error, make sure to provide all of the props that are required in the component. Here is an example of how the error occurs.

Is missing the following properties from type?

The error "Type is missing the following properties from type" occurs when the type we assign to a variable is missing some of the properties the actual type of the variable expects. To solve the error, make sure to specify all of the required properties on the object.

Does not exist on type {} TypeScript?

The "Property does not exist on type '{}'" error occurs when we try to access or set a property that is not contained in the object's type. To solve the error, type the object properties explicitly or use a type with variable key names.

Is not assignable to type TypeScript?

The "Type 'string' is not assignable to type" TypeScript error occurs when we try to assign a value of type string to something that expects a different type, e.g. a more specific string literal type or an enum. To solve the error use a const or a type assertion.


1 Answers

Try changing your cast to ResultPlus[] (the array type) not the single instance:

**this.dataArray** = (data as unknown as ResultPlus[]);

ie, you've declared dataArray to be an array of ResultPlus type.

If you're trying to add to this.dataArray for each item data, then you need to push to it - something like:

this.dataArray.push(data as unknown as ResultPlus);
like image 172
Jon Egerton Avatar answered Oct 15 '22 18:10

Jon Egerton