Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

TypeScript Argument of type 'IAccount' is not assignable to parameter of type 'never'

I am getting the above error when using the following types:

export interface IAccounts {
    "accounts": Array<IAccount>;
}

export interface IAccount {
  "username": string;
  "assets": Array<any>;
}

I am trying to initialize an account in my JSON file inside of a class constructor:

constructor(username: string) {
        super();
        this.username = username;
        if(!fs.existsSync('db.json')){
            let accountsObj = { "accounts": [] };
            let account: IAccount = {"username": username, "assets": []};
            accountsObj.accounts.push(account); // <-- here is where i am getting the error
            fs.writeFileSync('db.json', JSON.stringify(accountsObj), (err: any) => {
                if(err) {
                    console.log(err);
                }
                else {
                    console.log('************************************************************')
                    console.log(`*  Account successfully created. Welcome, ${this.username}!*`)
                    console.log('************************************************************')
                }
            })
            this.accountsObj = this.getAccountsObject();
            this.account = account;
        }
        else {
            this.accountsObj = this.getAccountsObject();
            this.account = this.getAccount(this.username);
        }
        
        
    }

I have tried to get this to work in multiple ways, I feel like I am missing something in the interface definitions.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You need to specify the type of accountsObj. Otherwise Typescript will not know the correct type and will asign { accounts: never[] } based on the assigned object to it.

let accountsObj: IAccounts  = { "accounts": [] }

// or

let accountsObj = { "accounts": [] } as IAccounts
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading