Skip to content
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

Added TypeScript definitions, updated babel to copy .d.ts files #76

Closed
wants to merge 1 commit into from

Conversation

tcarrio
Copy link

@tcarrio tcarrio commented Jan 4, 2020

  • TypeScript definitions added for all JavaScript files in project
  • Babel build script updated to include non-compiling (*.d.ts) files from src/ in lib/

This PR is the current work-in-progress for Issue #74

…o copy type definition files to lib/ in build script
@aero31aero
Copy link
Member

Hey, thanks a lot for working on this @tcarrio. This is looking really great! 🎉

How are you test these changes? Also, tell when you think this is ready for a review.

@tcarrio
Copy link
Author

tcarrio commented Jan 5, 2020

Most of the typing is based on the API so with unit testing it would be mocking successful/failed requests but it would still make the assumption of what those types are. I'm going to do some more work with the types as they are here to see that everything works as expected with the client itself, outside of the request/response types.

The TypeScript definitions are exported from the specific modules they were defined in with JavaScript. Certain methods don't exist in this client that I have found on the docs so they're stubbed with incomplete types. More would come with work on the client, which I may also take a further look at after this.

Would there be interest in converting the project to TypeScript entirely? It does alleviate concern for mismatching types. Figured I'd ask for a follow-up commit.

@timabbott
Copy link
Member

I'd be happy to convert this codebase to TypeScript; it'll still be usable from JavaScript code, and we're generally aiming to move all of our JavaScript codebases to TypeScript. And this is nicely small/encapsulated enough that we can reasonably switch it over and then verify that it works well.

@tommyip @andersk @showell FYI as they may have some thoughts on the details.

@tcarrio
Copy link
Author

tcarrio commented Jan 5, 2020

That's awesome news. I can take this change beyond TypeScript definitions to completely TypeScript while adhering to the existing tests to maintain the same functionality then.

@tcarrio
Copy link
Author

tcarrio commented Jan 7, 2020

This has a couple changes I would want to make first, but I am also working on a full TypeScript conversion that uses Babel still.

@andersk
Copy link
Member

andersk commented Jan 7, 2020

The existing code uses eslint, so we should configure typescript-eslint and make the *.ts files pass it. This adds 280 errors at present (179 automatically fixable formatting issues, 53 overlong lines, 36 import errors for zuliprc and types, 9 unexpected tab characters, 2 uses before definitions, and 1 prefer-default-export error that should maybe be suppressed).

diff --git a/.eslintrc b/.eslintrc
index 545ff1b..98cf819 100644
--- a/.eslintrc
+++ b/.eslintrc
@@ -1,5 +1,5 @@
 {
-  "extends": "airbnb",
+  "extends": "airbnb-typescript",
   "globals": {
     "fetch": false,
     "FormData": false
diff --git a/package.json b/package.json
index e02062a..b0186d2 100644
--- a/package.json
+++ b/package.json
@@ -22,21 +22,23 @@
     "@babel/plugin-transform-destructuring": "^7.6.0",
     "@babel/plugin-transform-instanceof": "^7.5.5",
     "@babel/preset-env": "^7.6.3",
+    "@typescript-eslint/eslint-plugin": "^2.15.0",
     "chai": "^4.2.0",
     "chai-as-promised": "^7.1.1",
     "eslint": "^6.5.1",
-    "eslint-config-airbnb": "^18.0.1",
+    "eslint-config-airbnb-typescript": "^6.3.1",
     "eslint-plugin-import": "^2.18.2",
     "eslint-plugin-jsx-a11y": "^6.2.3",
     "eslint-plugin-react": "^7.16.0",
     "mocha": "^6.2.1",
-    "sinon": "^7.5.0"
+    "sinon": "^7.5.0",
+    "typescript": "^3.7.4"
   },
   "scripts": {
     "build": "babel -D -d lib/ src/",
     "prepare": "npm run build",
     "prepublish": "npm run build",
-    "lint": "eslint src test examples",
+    "lint": "eslint src test examples --ext js,ts",
     "test": "npm run lint && npm run build && mocha test/*",
     "call": "node examples/interactive_call_endpoint.js"
   },

@andersk
Copy link
Member

andersk commented Jan 7, 2020

Also, this needs a tsconfig.json and some type packages for tsc to work.

diff --git a/package.json b/package.json
index b0186d2..6ff461b 100644
--- a/package.json
+++ b/package.json
@@ -10,6 +10,9 @@
     "fs-readfile-promise": false
   },
   "dependencies": {
+    "@types/isomorphic-fetch": "^0.0.35",
+    "@types/isomorphic-form-data": "^2.0.0",
+    "@types/node": "^13.1.4",
     "fs-readfile-promise": "^3.0.1",
     "ini": "^1.3.5",
     "isomorphic-fetch": "^2.2.1",
diff --git a/tsconfig.json b/tsconfig.json
new file mode 100644
index 0000000..e08f8b9
--- /dev/null
+++ b/tsconfig.json
@@ -0,0 +1,16 @@
+{
+  "compilerOptions": {
+    "target": "ES2019",
+    "noEmit": true,
+    "isolatedModules": true,
+    "strict": true,
+    "moduleResolution": "node",
+    "esModuleInterop": true,
+    "forceConsistentCasingInFileNames": true
+  },
+  "include": [
+    "src/**/*",
+    "test/**/*",
+    "examples/**/*"
+  ]
+}

@tcarrio
Copy link
Author

tcarrio commented Jan 7, 2020

I'll make sure to format all the updates accordingly. Currently this only copies over the TypeScript definitions so the TypeScript package isn't really necessary. But the switch to TS entirely will incorporate all of these

@andersk
Copy link
Member

andersk commented Jan 7, 2020

The typescript package is required for @typescript-eslint/eslint-plugin (used via eslint-config-airbnb-typescript). We should be type-checking and linting these files even if they’re just definitions. Doing so has already caught the missing @types/isomorphic-fetch, @types/isomorphic-form-data, @types/node dependencies, for example.

@tcarrio
Copy link
Author

tcarrio commented Jan 22, 2020

I'll have time to look at this in the next few days. Thanks for the heads up on those @andersk

@wiktor-obrebski
Copy link

hi. is there any progress with TS support for zulip-js? could it be merged?

@tcarrio
Copy link
Author

tcarrio commented Jul 27, 2020

Not yet @psychowico. I don't have as much time to work on this these days, if you wanted to pick it up. Otherwise, it may be some time.

Base automatically changed from master to main February 5, 2021 20:51
@sundargs2000
Copy link

sundargs2000 commented Mar 11, 2021

Hey. Can I pick this up?

@tcarrio
Copy link
Author

tcarrio commented Mar 11, 2021

Hey. Can I pick this up?

Feel free! I haven't had time to pick this up at all yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants