How to temporally define a Object type more specifically?

I am using Angular 2.

Usually, when I define a type, I create a interface first:

interface Product {
  name: string,
  count: number
}

then use like

let product: Product;

Now I want to temporally define a Object type. Something like this:

let product: Object<name: string, count: number>;

But this is wrong. How can I do it correctly? Thanks

1
Leave a Reply

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Jason Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Jason
Guest

You’re actually pretty close. It’s called Object literal type definition and it looks like this:

var product: { name : string; count: number; }
// Or if you don't like semi-colons, you can comma-delimit them
var product: { name : string, count : number }

// Later you could utilize it like normal

product = { name : 'joe', count : 5 };

Interfaces are just great because they could be reused everywhere