-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
ref(core): Make hint callback argument non-optional #5141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
6f155c1
to
887a30e
Compare
887a30e
to
b242a56
Compare
@@ -49,12 +49,12 @@ export class Debug implements Integration { | |||
consoleSandbox(() => { | |||
if (self._options.stringify) { | |||
console.log(JSON.stringify(event, null, 2)); | |||
if (hint) { | |||
if (Object.keys(hint).length) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixing these types to actually represent what is passed has highlighted that this would have always been logging an empty object for an empty hint.
Man, sometimes TS makes my brain hurt. type OneOrTwoParamCallback = (a: string, b?: string) => void;
type TwoParamCallback = (a: string, b: string) => void;
type OneOrTwoParamAccepter = (cb: OneOrTwoParamCallback) => void;
type TwoParamAccepter = (cb: TwoParamCallback) => void;
function oneParamCallback(a: string): void {}
function oneOrTwoParamCallback(a: string, b?: string): void {}
function twoParamCallback(a: string, b: string): void {}
// Currently, we have a OneOrTwoParamAccepter
const iTakeAOneOrTwoParamCallback: OneOrTwoParamAccepter = (
cb: OneOrTwoParamCallback
) => {
cb("first param")
cb("first param", "second param")
};
iTakeAOneOrTwoParamCallback(oneParamCallback);
iTakeAOneOrTwoParamCallback(oneOrTwoParamCallback);
iTakeAOneOrTwoParamCallback(twoParamCallback);
// After, we'll have a TwoParamAccepter
const iTakeATwoParamCallback: TwoParamAccepter = (cb: TwoParamCallback) => {
cb("first param", "second param")
};
iTakeATwoParamCallback(oneParamCallback);
iTakeATwoParamCallback(oneOrTwoParamCallback);
iTakeATwoParamCallback(twoParamCallback); My linter hates this, but it does compile, weirdly. In any case, you're correct that we don't break anyone by doing this - that erroring case is under the current scheme, not the new one. And it is nice not to have to |
Whichever of #5140 or this PR gets merged first, I'll clean the other one up before merging because it'll simplify the null checking. |
Now we allow mutating of properties in the hint to pass through things like attachments in
beforeSend
and Event processors, the hint is always defined, even if it's just an empty object.This PR removes the optional from the types which means users will no longer need to do unnecessary things like this to work around the incorrect types:
https://github.com/getsentry/sentry-javascript/pull/5140/files#diff-51dd65d366cb1690b4b8b45f0f835ae66f2a20b4d7ee77c0b6c1f2efd28dcb55R48-R54
Because theses are callback parameters and users can just leave off parameters they're not using, this is not a breaking change and means the types more accurately reflect what we're passing to the callback.
Even if a user has code with an optional parameter type defined:
their code will potentially be left with unnecessary null checks but will compile without errors.
I guess there could be lints I'm unaware of that check signatures match?