2

I'm creating new classes in Angular for data model.

I have created classes in new file and initialize them ins constructor

export class CreditPoliciesDetails { 
    private _fileName : string ="" ;
    private _fileDate : string ="" ;
    private _fileSize : string ="" ;
    private _path : string=""; 
    private _extention: string="";
    constructor() {
        this._fileDate="fileDate";
        this._fileName="fileName";
        this._fileSize="fileSize";
        this._path="path";
        this._extention="extension";
    }
}  

export class CreditPolicies {
    private _document : CreditPoliciesDetails[];
    private _directory: string
    constructor (details: CreditPoliciesDetails[]) {
        this._document = details;
        this._directory="";
    }
    get category() {
        return this._document
    }
    public details (detail) {
        console.log(detail)

        this._document= detail;
    }
}

Then in my component ts file I declare new CreditPoliciesDetails array then pass it to files which is an array of type of CreditPolicies

  details :CreditPoliciesDetails[] = new Array <CreditPoliciesDetails>(); 

  files : CreditPolicies[] = new Array <CreditPolicies>(this.details);

In VS code I get a warning When I hover the argument of new Array <CreditPolicies>(this.details); Argument of type 'CreditPoliciesDetails[]' is not assignable to parameter of type 'CreditPolicies'. Property '_document' is missing in type 'CreditPoliciesDetails[]'.

And when I console.log files and details I get empty array.

I'm expecting this format

[  
   {  
      "directory":"string",
      "document":[{  
         "extension":"string",
         "lastModifiedDate":"string",
         "name":"string",
         "path":"string",
         "size":"0"
      }]
   }
]
1
  • details is empty array in your case. You're providing empty array as new Array argument, while it's expected to be a number - and will result in empty files array too. I'd suggest to check how arrays work in JS, because they work in TS exactly the same way. Commented Apr 19, 2018 at 9:23

2 Answers 2

1

You have to call the constructor, not to cast the CreditPoliciesDetails array into CreditPolicies array

details :CreditPoliciesDetails[] = new Array <CreditPoliciesDetails>(); 

//files : CreditPolicies[] = new Array <CreditPolicies>(this.details);

files : CreditPolicies = new CreditPolicies(this.details);
Sign up to request clarification or add additional context in comments.

5 Comments

Files should be an array like in the expected format. How can I call the constructor for details and files and declaring them as arrays in the same time ?
what u can do is declare the array first, then initialize it 1 by 1 in an loop or so by calling the constructor
i think u r confused in structure some where, What i understand from the code is u have 1 file**(CreditPolicy) can have multiple **details(CreditPolicyDetails). if its true then its OK. means u have details :CreditPoliciesDetails[] = new Array <CreditPoliciesDetails>(); contains the details of 1 FILE
other situation is 1 detail have 1 file. means details :CreditPoliciesDetails[] = new Array <CreditPoliciesDetails>(); having 5 details and we want 5 files (CreditPolicy) then this is something else
Files is an Array of CreditPolicies and every _document could have n detail. for example : we have many directories , and every directory contain many files..
0

(this.details); Argument of type 'CreditPoliciesDetails[]' is not assignable to parameter of type 'CreditPolicies'.

It simply says, that you have in the

this.details

an arrray of the type CreditPoliciesDetails, that you are trying to pass to the constructor. Array used as a constructor in this case, expects arguments of type CreditPolicies. You are passing an array of CreditPoliciesDetails.

Also, I recomend using the following syntax for the previous code.

export class CreditPoliciesDetails { 
    constructor(
        private _fileDate="fileDate",
        private _fileName="fileName",
        private fileSize="fileSize";.....

) {}
}  

It spares time and makes the code more eligible.

2 Comments

I havn't understand this part Array used as constructor expects single values as parametres.
Array used as a constructor in this case, expects arguments of type CreditPolicies. You are passing an array of CreditPoliciesDetails.

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.