Issue
I have a util function whose first parameter param
must include (simplified for example) the properties param.X
and param.Y
, but it doesn’t matter what other properties it has. For instance it can also have param.Z
but that’s not used by the util function and can be ignored.
How can I type the param such that it enforces this constraint without complaining something like ParamType does not include property Z
?
Solution
Intersect the object type with Record<PropertyKey, unknown>
.
const fn = (obj: { x: string; y: string } & Record<PropertyKey, unknown>) => {
};
fn({x: 'x'}) // Fails
fn({x: 'x', y: 'y'}) // Passes
fn({x: 'x', y: 'y', z: 'z'}) // Passes
Answered By – CertainPerformance
Answer Checked By – Jay B. (BugsFixing Admin)