Home > Back-end >  How to take a copy of nullable object in TypeScript to keep type?
How to take a copy of nullable object in TypeScript to keep type?

Time:02-21

Is it any operator, or generic solution to take copy of nullable object, and the result should be the same nullable type?

Like here, but ... spread operator will not retain type.

const [startPaymentIn, setStartPaymentIn] = useState<
  StartPaymentIn | undefined
>(undefined);

let startPaymentIn2: StartPaymentIn | undefined = {
  ...startPaymentIn,
};

CodePudding user response:

This can retain the type...

startPaymentIn && {...startPaymentIn}
  • Related