0

I am trying to get the length from Array interface. But getting error as:

error TS2339: Property 'length' does not exist on type 'ModelSubSystems'. In this case how to get the length. here is my TS code:

@Input() subsystems: ModelSubSystems;

resolve({ data: this.subsystems, totalCount: this.subsystems.length }); //error

here is my interfaces:

export interface ModelSubSystem {
    Id: number;
    Name: string;
    CreatedBy: string;
    CreatedDate: Date;
    UpdatedBy: string;
    UpdatedDate: Date;
}

export interface ModelSubSystems {
    subsystems: Array<ModelSubSystem>; //this is array!!
}

any one help me with correct approach. Thanks in advance.

2
  • 1
    Do you want this.subsystems.length, or this.subsystems.subsystems.length? Commented Jun 17, 2019 at 5:19
  • @JeffBowman - I tried with declaring @Input() subsystems: ModelSubSystems.subsystem; but got the error as @Input() subsystems: ModelSubSystems.subsystem; But getting warning as ModelSubSystems only refer to a type, but being used as a name space here Commented Jun 17, 2019 at 5:35

2 Answers 2

3

@Input() subsystems: ModelSubSystems this input variable will not have length because it is an interface with one property whose name is subsystems as well and is an array(the one you are trying to get length of).

The array property created in the interface can be accessed using this.subsystems.subsystems while the first subsystem here is you input of type systemsystem and the second one is the array in that interface.

If you want the subsystems input to be to be an array of ModelSubSystem, instead of creating ModelSubSystems interface, directly declare this input as and array so that you can access its length:

@Input() subsystems: Array<ModelSubSystem>
Sign up to request clarification or add additional context in comments.

2 Comments

I declared like this: @Input() subsystems: ModelSubSystems.subsystem; But getting warning as ModelSubSystems only refer to a type, but being used as a name space here
But sad to see why my ModelSubSystems not helped here.
1

Are you certain that the this.subsystems variable is initialised instead of only declared?

Typescript interface variables are undefined by default, so you will have to set them before they can be used / their methods can be called.

EDIT: as Jeff Bowman pointed out, what you actually need is this.subsystems.subsystems.length.

You could also add the length property to the interface, and have a class implement the desired behavior:

export interface IModelSubSystems {
  subsystems : Array<ModelSubSystem>;
  length : number;
}

export class ModelSubSystems implements IModelSubSystems {
  subsystems = new Array<ModelSubSystem>();
  get length() : number {
    return this.subsystems.length;
  }
}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.