-
Notifications
You must be signed in to change notification settings - Fork 23
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
Add TypeScript support #104
Conversation
This process was based on a TypeScript official guide: https://www.typescriptlang.org/docs/handbook/declaration-files/dts-from-js.html.
This is a mandatory steps to add support for TypeScript without migrating the entire library. More details: https://jsdoc.app.
Hi @CharlesMangwa , Thanks a lot for implementing support for TypeScript and creating this PR. However, when we try to
May you fix this? |
Thanks for the feedback! It was an oversight on my end which should be resolved now: # cd sample && npm i
added 1243 packages, and audited 1245 packages in 9s
143 packages are looking for funding
run `npm fund` for details
found 0 vulnerabilities |
Hi @CharlesMangwa , It is working now when using dependency Unfortunately using the package as local module doesn't work on machines that do not have TypeScript installed globally.
It might be better to revert the last commit and keep sample project as is. |
Hey @biancalui-emarsys! Thanks for the heads up, change removed! ✅ |
Motivation 💪
We're planning on using Emarsys at colorfy for some of our projects. However, the lack of static typing is a major hindrance for us, as all of them are fully typed. That what birthed this PR with the goal to implement support for TypeScript.
Approach 🤔
While making this PR, we tried to keep in mind the desire not to disturb how the library has been built so far. That's why we decided to keep the library written in JavaScript and simply make use of the existing comments to generate the equivalent TypeScript
.d.ts
declaration files.Note
Doing so is possible thanks to a feature introduced in TypeScript 3.7. Our process described below was based on a TypeScript official guide.
Therefore, our approach was to:
.d.ts
declaration files.The main advantage of proceeding as such is that you will not have to modify anything major in how you were building this library up until now.
Warning
The only noticeable change is a rather small one: you'll just have to make sure to comply to the JSDoc standards when writting comments from now on.
Hopefully, this won't be much of a chore as the current comments were already really close to it.
Eg:
Disclaimers 📣
We also marked in the JSDoc comments all the
Predict.recommend...()
methods that are@deprecated
in a way that the compiled TypeScript interfaces will complain at any further use, even accidental. A link to the newPredict.recommendProducts()
method was also added.Incidentally, we also marked
Push.pushToken()
as@deprecated
and duplicated it into a newPush.getPushToken()
. This naming seemed more logical in the context of the other existing methods:Push.setPushToken()
&Push.clearPushToken()
. Of course, no functional change was made to the existingPush.pushToken()
while doing so.Build 🏗️
The build process has received very minimal modifications.
Important
The sole new requirement is for your machines to have TypeScript's
tsc
compiler installed. This can be done in various ways; the one this PR implements is adding TypeScript as adevDependencies
.From there, any time the package is being packed (or the command
tsc
is being run in the root folder), the compiler will be looking at 2 paths:./index.js
&./src/**/*
(as instructed in./tsconfig.json#L3
).If it finds any JavaScript files there, all the
.d.ts
declaration files and their respective source maps will be generated in a newly created./typescript
folder.When the library is consumed by a client, the new
"types": "typescript/src/index.d.ts"
line in thepackage.json
will let TypeScript-enabled projects know where to find the declaration files.Tip
Once again, just make sure to adhere to the JSDoc standards to keep the process running smoothly even when adding new APIs.