I would like to type a DB insert function that allows defaults: (I am using SetOptional of type-fest)
function defaultInsert<T, D = Partial<T>>(data: SetOptional<T, keyof D>, defaults: D) {
What I am trying to say with the SetOptional is that the data object should contain anything in T, not specified in defaults (D) but it is allowed to overwrite anything in the defaults.
Sadly this is resulting in the following error
Type 'keyof D' does not satisfy the constraint 'keyof T'.
Type 'string | number | symbol' is not assignable to type 'keyof T'.
Type 'string' is not assignable to type 'keyof T'.(2344)
Typescript playground for a more elaborate example.
Is there any way I can filter down the Partial to only contain keys of T since I'm assuming this is the problem due to the possibility of D containing excess properties not in T?
Many thanks
EDIT: As a suggestion from @Alex Chashin, another TS Playground using the keys as a way to define the defaults.
EDIT2: Updated playground with solution provided in UPD2 @Alex Chashin
EDIT3: Updated the previous example so that defaults can also be omitted: playground