Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

204
Views
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 ?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You're looking for string[]. However, it doesn't look like your example is a type definition?

I would write it like this:

let someObject: { somePropety: { A: number; B: number; C: string[] } } = {
        somePropety: {
          A: 0,
          B: 0,
          C: []
        }
}; 
about 4 years ago · Juan Pablo Isaza Report

0

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.

about 4 years ago · Juan Pablo Isaza Report

0

If you are trying to define C as an array of strings, you can do:

C: string[];

or using a generic array type:

C: Array<string>

You can find more detail on TypeScript basic types here

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!