-
Notifications
You must be signed in to change notification settings - Fork 17
Open
Description
problem: If I send the payload with extra fields, then I get an error in the rest. rest does not expect extra fields.
interface ProductModify {
name: string;
}
interface Product extends ProductModify {
id: string;
foo: number;
}
const product: Product = { name: 'TEST', id: '1', foo: 1 };
const modifyProduct = (product: ProductModify) => {};
modifyProduct(product); // no type errors, because Product compatible with ProductModify
I propose to prohibit extra fields, i.e. disable polymorphism.
type Impossible<K extends keyof any> = {
[P in K]: never;
};
export type NoExtraProperties<T, U extends T = T> = U & Impossible<Exclude<keyof U, keyof T>>;
const modifyProduct = <T extends ProductModify>(product: NoExtraProperties<ProductModify, T>) => {};
modifyProduct(product); // type error: Argument of type 'Product' is not assignable to parameter of type 'NoExtraProperties<ProductModify, Product>'
Metadata
Metadata
Assignees
Labels
No labels