Replies: 1 comment 2 replies
-
I just updated to 1.1.0-beta.5 and I've encountered some issues regarding the:
I think it's a huge breaking change (not it terms of behavior but in terms of types :)) due to how the In TypeScript I can pass a type User = {
id: string;
email: string;
name: string;
};
const getUserEmail = (user: User) => user.email;
const useUser = (id: string) => useSWR<User>(`.../${id}`);
const UserComponent: React.VFC = () => {
const { data: user } = useUser("123");
// ^ user will be of type `Readonly<User> | undefined`
// I can pass `user` to `getUserEmail` email without any type check error
const userEmail = user ? getUserEmail(user) : "";
return <>{userEmail}</>;
}; That is not true when using arrays. I cannot pass a type ConnectedUsers = Array<User>;
const getConnectedUsersEmails = (users: Array<User>) =>
users.map((u) => u.email);
const useConnectedUsers = () => useSWR<ConnectedUsers>(`...`);
const ConnectedUsersComponent: React.VFC = () => {
const { data: users } = useConnectedUsers();
const usersEmails = users ? getConnectedUsersEmails(users) : [];
// ^ Argument of type 'readonly User[]' is not assignable to parameter of type 'User[]'
return (
<>
{usersEmails.map((email) => (
<div>{email}</div>
))}
</>
);
}; This is a pain because if we want to use the Check out this codesandbox to see the TS errors: https://codesandbox.io/s/useswr-readonly-return-type-issues-sqyzn?file=/src/App.tsx In our project we enforce immutability by using the eslint functional plugin, specifically with |
Beta Was this translation helpful? Give feedback.
-
Minor Changes
Patches
Credits
Huge thanks to @timas130 and @huozhi for helping!
This discussion was created from the release 1.1.0-beta.5.
Beta Was this translation helpful? Give feedback.
All reactions