I am facing an issue with my updateView function that is supposed to merge changes and update a view on the backend. The problem lies in not properly restricting what changes can be passed in based on the type of view. Below is an example of the code snippet:
How do I modify the UpdateView interface to enforce strict restrictions on the allowable changes?
interface BaseView {
immutableProp?: string;
mutableProp?: string;
name: string;
isCustom: boolean;
}
interface StandardView extends BaseView {
isCustom: false;
}
interface CustomView extends BaseView {
isCustom: true;
}
type View = StandardView | CustomView
type StandardMutableFields = Partial<Pick<StandardView, "mutableProp">>
type CustomMutableFields = Partial<Pick<CustomView, "mutableProp" | "name">>
interface UpdateView {
<T extends View>(viewToChange: T, changes: T extends CustomView ? CustomMutableFields : StandardMutableFields): T
}
const updateView: UpdateView = (viewToChange, changes) => {
const updatedView = { ...viewToChange, changes };
// SAVE TO SERVER
return updatedView;
}
const changeName = (view: View, name: string) => {
updateView(view, { name: "this is allowed but shouldn't be" }) // should require a check to ensure the view is custom
}