Skip to content

Instantly share code, notes, and snippets.

@xavhan
Created December 1, 2021 14:02
Show Gist options
  • Save xavhan/06d0966fd827485755f68488c3015082 to your computer and use it in GitHub Desktop.
Save xavhan/06d0966fd827485755f68488c3015082 to your computer and use it in GitHub Desktop.
Use TS to force document your deprecations
const log = true; // comming from logging service (dont log in prod)
//
type Deprecation = {
what: string;
because: string;
recommendation: string;
adr_link: string;
}
const deprecated = (deprecation: Deprecation) => {
if (log) {
const { what, because, recommendation, adr_link } = deprecation;
const message = `[DEPRECATED] ${what} is deprecated because ${because}. We recommand you to ${recommendation}. (${link})`;
console.error(message);
}
};
//
function oldway(): void {
deprecated({
what: 'oldway',
because: 'it was buggy',
recommendation: 'use newway',
adr_link: 'http://example.com/adr/123',
})
/** old logic */
}
function newway(): void {
/** new logic */
}
@Amirault
Copy link

Amirault commented Dec 2, 2021

Interesting !

I love the fact we can add it easily inside all functions and the explanation : what, because, recommendation, adr_link.

I wondering how do you manage the log boolean : how do you inject it ? Maybe a use of a curry function or default param can be used for managing the context what do you think ?

I was think in another way too by using a js decorator ( @Deprecated("") )

or a curry function (

const deprecated = (deprecation) => {
//deprecation things
(deprecatedFunction) => deprecatedFunction()
}

but not sure is better than your implementation.

@xavhan
Copy link
Author

xavhan commented Dec 6, 2021

@Amirault actually, how log value is set/injected depend on how your system is built and not really relevant here for the snippet
Let just say that this deprecated method could be a method exposed by your logging service and inherit of its settings

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment