useReducer with TypeScript

What's the best way to write this useReducer in TypeScript:
const [{ firstName, lastName }, setName] = useReducer(
(prev, next) => ({ ...prev, ...next }),
{
firstName: "",
lastName: ""
}
);
const [{ firstName, lastName }, setName] = useReducer(
(prev, next) => ({ ...prev, ...next }),
{
firstName: "",
lastName: ""
}
);
I was thinking this, but I wonder if there's a better way.
type TName = { firstName: string; lastName: string };
const [{ firstName, lastName }, setName] = useReducer(
(prev: TName, next: Partial<TName>) => ({ ...prev, ...next }),
{
firstName: "",
lastName: ""
}
);
type TName = { firstName: string; lastName: string };
const [{ firstName, lastName }, setName] = useReducer(
(prev: TName, next: Partial<TName>) => ({ ...prev, ...next }),
{
firstName: "",
lastName: ""
}
);
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server