Skip to content

prohibit extra fields in request payload #182

Description

@Geksanit

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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions