I have a function that includes optional parameters:
export const sentryException = (
error: Error,
contextLabel?: string,
contextInfo?: Context,
specificTags?: SentryTags,
) => {
The last three parameters are optional. However, if contextLabel
is provided, then contextInfo
must also be provided. I would like to avoid having to do the following check:
if ((contextLabel && !contextInfo) || (contextInfo && !contextLabel)) {
// throw error
}
Managing multiple required combinations of inputs can become cumbersome!
Is there a way to enforce that a subset of optional parameters must be passed in when a specific parameter is provided?
Edit:
I want to clarify that the // throw error
comment does not refer to the error
parameter. It means that both contextLabel
and contextInfo
must always be defined (not undefined
) for the function to execute.
For simplicity, let's say all types are strings except for error
. Valid function calls could look like:
sentryException(err, 'a', 'b')
sentryException(err, 'a', 'b', 'c')
sentryException(err, undefined, undefined, 'c')
sentryException(err)