13

I am attempting to create a variable within an in-memory database that contains a TypeScript interface that describes a JSON dataset. This dataset should contain multiple arrays, which in turn contain multiple objects with a fixed length and consistent attributes.

I am writing this in Angular4 and TypeScript. The arrays must be of variable length with a minimum of 1 member

I've written this pseudo code to show you what I mean:

export class MyHomeBrewery {
  taps: Array<any> = [{
    id: number;
    name: string;
    type: string;
    quantity: number;
  }][...];
  barrels: Array<any> {
    id: number;
    name: string;
    width: number;
    height: number;
    quantity: number;
 }][...];

I've had a look through the TypeScript and Angular documentation and done a few searches and I can't find the correct syntax for this. Does anyone know?

0

1 Answer 1

24

You should be using them as interfaces as below

export interface MyHomeBrewery {
    taps: Array<Taps>;    
    barrels: Array<Barrels>;
}

export interface Taps {

    id: number;
    name: string;
    type: string;
    quantity: number;
}

export interface Barrels {
    id : number;
    name: string;
    width: number;
    height: number;
    quantity: number;
}
Sign up to request clarification or add additional context in comments.

5 Comments

Of course. Also congrats on what I think is the fastest reply I've ever seen on here.
Thank you. so is this what you were looking for?
It definitely looks like it. It's kind of ironic that somehow I knew what I was looking for was called an interface but couldn't find the documentation that told me I had to use the interface keyword for it. I'll give it a try in a bit and accept + upvote if it works. Again, props on the speedy reply.
Yes. What is that you are trying and what error you get?. This is will work for you. when you are assigning values you need to do it just like any other object.
Fantastic answer to a well-written question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.