25

I would like to specify a function that takes an array of objects as a parameter, but I don't have a particular type defined for the object (a sort of "anonymous type")

bagTotal = (products) => {
 // function does stuff
}

I understand I can do this:

bagTotal = (products: any[]) => {
 // function does stuff
}

but this is a bit more relaxed then what I want: to be strict with typescript.

products is an array of the same-looking objects; all objects have a name, a price and a description.

how can I declare that?

I want to do something like

bagTotal = (products: [{name: string, price: number, description: string}]) => {
 // function does stuff
}

but that's not right. How can I declare this?

Red Baron
  • 7,181
  • 10
  • 39
  • 86

4 Answers4

67

You're almost there, the placement of the brackets is just wrong:

{name: string, price: number, description: string}[]

The way you had it isn't entirely wrong, but it means something else: it means an array with exactly one item of this type.


I'd also recommend extracting it to an interface, it'd make the type reusable and the thing easier to read:

interface Product {
    name: string;
    price: number;
    description: string;
}

const products: Product[];
Ingo Bürk
  • 19,263
  • 6
  • 66
  • 100
  • 4
    I would recommend using the `Array` form if the item type is very large : `Array<{name: string, price: number, description: string}>` – Titian Cernicova-Dragomir Dec 28 '18 at 22:03
  • 1
    I just added that I'd actually extract a type for it anyway ;) – Ingo Bürk Dec 28 '18 at 22:04
  • @IngoBürk I slighted edited my question. please see. I can accept your answer if you answer that, thanks – Red Baron Dec 28 '18 at 22:15
  • @Red Baron products in your interface is still untyped, so what's the point? Also I'm rolling back that edit. Please don't change the question fundamentally after receiving answers. You asked a question, it has been answered. For a new question please open a new question. – Ingo Bürk Dec 28 '18 at 22:20
  • ok I'll open a new question when it allows me too and accept this one. thanks – Red Baron Dec 28 '18 at 22:21
6

The generic array type Array<elemType> provides a straightforward way to declare an array of objects:

bagTotal = (products: Array<{name: string, price: number}>) => {
   ...
}
Shaya
  • 2,792
  • 3
  • 26
  • 35
5

I think you must declare the class "Product" so you can declare a Product array like this:

products: Product[];

and pass it as a parameter like this:

bagTotal = (products: Product[]) => {
 // function does stuff
}

To have the class you can do a new .ts file with this code:

export class Product {
  name: String;
  price: Number;
  description: String;
}

I wish that helped!

Thank you.

1

If you are declaring an array of a specific object and want to specify type for variables in the objects, I would create a class for the object like this:

class Item(){
    name: string;
    description: string;
    etc: any

  constructor() {
    this.name;
    this.description;
    this.etc;
}}

Then you can specify the array as an array of item objects:

itemArray: Array<Item>;
Sockness_Rogers
  • 1,693
  • 3
  • 12
  • 23