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

How to predefine object with element as an array <string> type?

I have an object, and i need to define in this object sub element, which will be as an array.
But how do i define it’s type?
Without type definition it throws error.

Implicitly has any[]

let someObject = {
  somePropety: {
    A : 0,
    B : 0,
    C : []<string>,
  }
}

Does anyone know how to asign C element an array of string type ?

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 seem to be looking for

let someObject = {
  someProperty: {
    A: 0,
    B: 0,
    C: [] as string[],
  },
};

Alternatively, you can declare the type of the entire someObject variable (see @wooooooo’s or @ksav’s answers for that) instead of using a type assertion on the empty array literal.

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