Typescript: Type X is missing the following properties from type Y length, pop, push, concat, and 26 more. [2740]

mpro
:

I have this Product interface:

export interface Product{
  code: string;
  description: string;
  type: string;
}

Service with method calling product endpoint:

  public getProducts(): Observable<Product> {
    return this.http.get<Product>(`api/products/v1/`);
  }
  

And component where I use this service to get the Products.

export class ShopComponent implements OnInit {
    public productsArray: Product[];
    
    ngOnInit() {
        this.productService.getProducts().subscribe(res => {
          this.productsArray = res;
        });
    }
}

With this state I'm getting error:

[ts] Type 'Product' is missing the following properties from type 'Product[]': length, pop, push, concat, and 26 more. [2740]

Removing typing on productsArray variable removes the error, but don't get why this is not working, since server response is an array of objects in the type of Products?

Amit Chigadani
:

You are returning Observable<Product> and expecting it to be Product[] inside subscribe callback.

The Type returned from http.get() and getProducts() should be Observable<Product[]>

public getProducts(): Observable<Product[]> {
    return this.http.get<Product[]>(`api/products/v1/`);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Why does TypeScript infer the 'never' type when reducing an Array with concat?

From Java

remove null or undefined from properties of a type

From Dev

TypeScript + useRef<X|Y>: Type X is not assignable to type Y

From Dev

TypeScript conditional type resolution and optional properties

From Dev

The type X in Y conflicts with the imported type X in Z

From Dev

Confused by the following data type

From Dev

C# #import referenced a type from a missing type library; '__missing_type__' used as a placeholder

From Dev

Error 26 Cannot implicitly convert type 'object' to 'string'. An explicit conversion exists (are you missing a cast?)

From Dev

The method newCompatibleTransport() from the type AndroidHttp refers to the missing type

From Dev

How to remove TypeScript warning: property 'length' does not exist on type '{}'

From Dev

TypeScript union type requires length

From Dev

vector::push_back conversion from const Type* to type*

From Dev

Don't push properties of an element which type is null

From Dev

How to: Select Distinct objects<T> from multiple Lists<T> with 1 or more properties of same type

From Dev

React + Typescript: Property * is missing in type *

From Dev

X is not a member type of Y

From Dev

TypeScript ReactDOM Property 'type' is missing on component

From Dev

Typescript: Index signature is missing in type

From Dev

Importing TypeScript type from another TypeScript package

From Dev

Declaring object of arrays in Typescript; getting error Type '{}' missing the following properties

From Dev

Missing properties in IgniteUI after upgrade from TypeScript 1.0 to 1.8

From Dev

Typescript conditional type missing properties

From Dev

TypeScript React Functional Component - is missing the following properties from type 'Element': type, props, key error

From Dev

Type Error while using interval and map with RXJS in Typescript. errorcode: ts(2740)

From Dev

How to define type of an object whose properties are going to be fetched from an array in TypeScript

From Dev

Typescript type mapping from Interface

From Dev

Typescript - create type of all available css properties

From Dev

Detect fixed length tuple type vs. array type in TypeScript

From Dev

Typescript error: Property 'children' is missing in type but required in type 'CommonProps'

Related Related

  1. 1

    Why does TypeScript infer the 'never' type when reducing an Array with concat?

  2. 2

    remove null or undefined from properties of a type

  3. 3

    TypeScript + useRef<X|Y>: Type X is not assignable to type Y

  4. 4

    TypeScript conditional type resolution and optional properties

  5. 5

    The type X in Y conflicts with the imported type X in Z

  6. 6

    Confused by the following data type

  7. 7

    C# #import referenced a type from a missing type library; '__missing_type__' used as a placeholder

  8. 8

    Error 26 Cannot implicitly convert type 'object' to 'string'. An explicit conversion exists (are you missing a cast?)

  9. 9

    The method newCompatibleTransport() from the type AndroidHttp refers to the missing type

  10. 10

    How to remove TypeScript warning: property 'length' does not exist on type '{}'

  11. 11

    TypeScript union type requires length

  12. 12

    vector::push_back conversion from const Type* to type*

  13. 13

    Don't push properties of an element which type is null

  14. 14

    How to: Select Distinct objects<T> from multiple Lists<T> with 1 or more properties of same type

  15. 15

    React + Typescript: Property * is missing in type *

  16. 16

    X is not a member type of Y

  17. 17

    TypeScript ReactDOM Property 'type' is missing on component

  18. 18

    Typescript: Index signature is missing in type

  19. 19

    Importing TypeScript type from another TypeScript package

  20. 20

    Declaring object of arrays in Typescript; getting error Type '{}' missing the following properties

  21. 21

    Missing properties in IgniteUI after upgrade from TypeScript 1.0 to 1.8

  22. 22

    Typescript conditional type missing properties

  23. 23

    TypeScript React Functional Component - is missing the following properties from type 'Element': type, props, key error

  24. 24

    Type Error while using interval and map with RXJS in Typescript. errorcode: ts(2740)

  25. 25

    How to define type of an object whose properties are going to be fetched from an array in TypeScript

  26. 26

    Typescript type mapping from Interface

  27. 27

    Typescript - create type of all available css properties

  28. 28

    Detect fixed length tuple type vs. array type in TypeScript

  29. 29

    Typescript error: Property 'children' is missing in type but required in type 'CommonProps'

HotTag

Archive