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

132
Views
TS how can I this function write to a arrow function?

How can I write this generic function with a arrow function ?

function simpleState<T>(initial: T): [() => T, (v: T) => void] {
  let value: T = initial;
  return [
    () => value,
    (v: T) => {
      value = v;
    }
  ]
};
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

const simpleState = <T>(initial: T): [() => T, (v: T) => void] => {
  // same function body as before
}

Since you tagged react-native, be aware that in a .tsx file, this will probably not work, since the <T> will be parsed as being a JSX tag. You can either put it in a .ts file, and import it into a .tsx file, or use one of the following workarounds to get it to not be interpreted as a JSX element.

Trailing comma:

<T,>(initial: T): // etc

Extend from unknown:

<T extends unknown>(initial: T): // etc
about 4 years ago · Juan Pablo Isaza Report

0

const simpleState2 = <T>(initial: T): [() => T, (v: T) => void] => {
  let value: T = initial;
  return [
    () => value,
    (v: T) => {
      value = v;
    },
  ];
}
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!