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

Issue

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 ?

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.

Answered By – Bergi

Answer Checked By – Marilyn (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *