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

83
Views
Setting type for useState array of objects

I'm struggling to figure out what the problem is with my code.

interface history { 
  data: string, 
  przed: string | number, 
  po: string | number,
}

I have an interface outside of the Functional Component then inside I define how it's supposed to look like - no problem

const [history, setHistory] = useState<[history]>([{
    data: '',
    przed: '',
    po: '',
}]);

then I have a onClick function which does this:

    setHistory([ ... history , {
      data: new Date().toISOString().slice(0, 10), // date
      przed: fromTo, // number
      po: valueToSave, // number
    }]);

Everything works fine when compiled and tested in the browser, however VS Code is not liking my code at all with this error:

Argument of type '[history, { data: string; przed: number; po: string; }]' is not assignable to parameter of type 'SetStateAction<[history]>'.
  Type '[history, { data: string; przed: number; po: string; }]' is not assignable to type '(prevState: [history]) => [history]'.
    Type '[history, { data: string; przed: number; po: string; }]' provides no match for the signature '(prevState: [history]): [history]'.ts(2345)

How can I handle something like this? What does it mean?

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

0

You can use either history[] or Array<history> instead of [history] which means you want an array of history type or an array in which each elements will be of type history.

[history] means that it has an array that will contain an element of type history

history[] means you it is an array of type history

const [history, setHistory] = useState<history[]>([
    {
      data: "",
      przed: "",
      po: ""
    }
  ]);

or you can also do as:

  const [history, setHistory] = useState<Array<history>>([
    {
      data: "",
      przed: "",
      po: ""
    }
  ]);
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!