I have a user object with properties like id and name.
interface User {
id: string;
name: string;
}
There is also an UpdateUserBody type that allows updating only the 'name' property of the user in the backend.
type UpdateUserBody = Pick<User, 'name'>;
My goal is to create an array of functions that specifically return a single property of the UpdateUserBody.
Initially:
[
() => user.name
] as Array<() => keyof UpdateUserBody>
The issue arises when I try to add () => user.id
to the array, as it doesn't throw any error:
[
() => user.name,
() => user.id,
] as Array<() => keyof UpdateUserBody>
Explanation behind my approach
I'm utilizing Vuejs watchers to keep track of these properties for saving a draft of a new user being created by the logged-in user. This is done through:
const unwatchUserUpdateRequest = watch(
[
() => user.name,
] as Array<() => keyof UpdateUserBody>,
updateUserApiRequest,
)
I've attempted various methods but haven't been able to resolve this issue yet. Any suggestion on how to constrain the array to only accept functions returning properties of UpdateUserBody?