Created
December 1, 2021 14:02
-
-
Save xavhan/06d0966fd827485755f68488c3015082 to your computer and use it in GitHub Desktop.
Use TS to force document your deprecations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 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
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 (
but not sure is better than your implementation.