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

89
Views
union for array of object in typescript?

How to extends an property in array of object in typescript?

I have below react code:

interface List {
  id: string;
}
interface AppProps {
  list: List[];
}

const App: React.FC<AppProps> = ({ list }) => {
  const [listCustom, setListCustom] = React.useState<List[]>([]);

  React.useEffect(() => {
    setListCustom([
      {
        id: "3",
        newProperty: "new" //issue is here, how to extend List to have newProperty without modifying List?
      }
    ]);
  }, []);

  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <h2>Start editing to see some magic happen!</h2>
    </div>
  );
};


export default App;

In here

React.useEffect(() => {
    setListCustom([
      {
        id: "3",
        newProperty: "new" //issue is here, how to extend List to have newProperty without modifying List?
      }
    ]);
  }, []);

I have to set new property to List, but I don't want to modify List because it's tied to the AppProps. How can I 'extends' it in line const [listCustom, setListCustom] = React.useState<List[]>([]);?

It doesn't make sense to create a duplicated List2 like

interface List2 {
  id: string;
  newProperty: string
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can either extend the list interface:

interface List2 extends List {
  newProperty: string;
}

Or you can use an intersection type:

type List2 = List & { newProperty: string }

The interface can't be done inline, but the intersection can be:

const [listCustom, setListCustom] = React.useState<(List & { newProperty: string })[]>([]);
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!