Skip to content

Add TypeScript definitions for the api #536

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
sublimator opened this issue Feb 12, 2017 · 23 comments
Closed

Add TypeScript definitions for the api #536

sublimator opened this issue Feb 12, 2017 · 23 comments

Comments

@sublimator
Copy link
Member

Actually, there exists a @types/mqtt (which happens to be inconsistent with this latest version) but of course it would be ideal to have it in this repo (which is considered the best practice I believe).

@mcollina
Copy link
Member

Disclaimer: I am not a typescript user, and there is no one in the maintainers.
If you want to join and help, you are welcome!

In #386 it was discussed this matter. I'm happy to embed the type definition here, if you want to help maintaining that.

cc @PekkaPLeppanen

@sublimator
Copy link
Member Author

sublimator commented Feb 13, 2017 via email

@mcollina
Copy link
Member

@sublimator send a PR, I'll add you then.

@sublimator
Copy link
Member Author

k, once we got the other one sorted to your satisfaction, I'll get onto it :)

@ghost
Copy link

ghost commented Feb 17, 2017

I am using mqtt.js with TypeScript. I can contribute if you need my help.

@sublimator
Copy link
Member Author

sublimator commented Feb 17, 2017 via email

@PekkaPLeppanen
Copy link
Contributor

I wrote the type definitions for the MQTT API v1. I was hoping that one day I'll write another definitions for v2 API also. Because a lot of time has passed and nothing happened from my part, I must say it would be great if someone else could do the new type definitions.
I am no longer using MQTT, because I changed to another company to a completely different type of project.

Anyway, to write the type definitions, you should take the API documentation and write the definitions from there. The missing parts can be found from the source. And make PR for the documentation, if there is something missing.

Also notice that the two versions of the type definitions can co-exist, if the API v1 is still maintained actively. If it's not maintained, one should rewrite the existing file.

@sublimator
Copy link
Member Author

I've been playing with async/await in TypeScript 2.1 and it's lovely. It would be nice to Promise-able the API while we're at it.

@ghost
Copy link

ghost commented Mar 28, 2017

And DefinitelyTypes repo must be updated as well.
https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/mqtt

@ghost
Copy link

ghost commented Mar 28, 2017

I installed mqtt 2.5.0 by "npm install mqtt", but type definition files are not provided.

@mcollina
Copy link
Member

mcollina commented Apr 1, 2017

this should be done now in 2.5.1

@mcollina mcollina closed this as completed Apr 1, 2017
@yohei1126
Copy link
Member

@sublimator
I got the error "error TS2307: Cannot find module 'mqtt'." when compile. How can I resolve the module?

@sublimator
Copy link
Member Author

Can you verify that you have the typings in your node_modules/mqtt ? Are you using @mcollina's latest npm push of mqtt? What version of typescript?

@ghost
Copy link

ghost commented Apr 6, 2017

  • I have "\node_modules\mqtt\types".
  • I am using the follwoing modules.
    "mqtt": "^2.5.2",
    "typescript": "^2.2.2",

@sublimator
Copy link
Member Author

Interesting, as it "works for me" here.

import { connect, MqttClient } from 'mqtt'

Is all I'm using.

@ghost
Copy link

ghost commented Apr 6, 2017

My tsconfig.json is as follows.

{
  "compilerOptions": {
    "lib": ["dom", "es2015.promise", "es2015.collection", "es5", "es6"],
    "noImplicitAny": true,
    "noImplicitReturns": true,
    "noImplicitThis": true,
    "noUnusedLocals": true,
    "alwaysStrict": true,
    "strictNullChecks": true,
    "sourceMap": true,
    "types": [
        "mocha"
    ],
    "outDir": "./dist"
  },
  "exclude": [
      "node_modules"
  ],
  "include": [
     "src/**/*.ts",
     "test/**/*.ts"
  ]
}

The result of trace resolution is like this. The tsc compiler is trying to search the module in node_modules/@types/mqtt.d.ts. But not in node_modules/mqtt/types/index.d.ts.
Maybe this is an related to module resolution setting in tsconfig.json.

$ $(npm bin)/tsc -p . --traceResolution
======== Resolving module 'mqtt' from 'D:/work/xx.ts'. ========
Module resolution kind is not specified, using 'Classic'.
....
File 'D:/work/iotc_msgpub_js/node_modules/@types/mqtt.d.ts' does not exist.
...

@sublimator
Copy link
Member Author

sublimator commented Apr 6, 2017 via email

@sublimator
Copy link
Member Author

sublimator commented Apr 6, 2017 via email

@sublimator
Copy link
Member Author

sublimator commented Apr 6, 2017 via email

@ghost
Copy link

ghost commented Apr 6, 2017

Thanks. I fixed it with the "moduleResolution: Node" in the "compilerOptions".

{
  "compilerOptions": {
    "moduleResolution": "Node"
  }
}

@sublimator
Copy link
Member Author

We should update the TypeScript section of the README I guess

@mcollina
Copy link
Member

mcollina commented Apr 6, 2017

PR?

@sublimator
Copy link
Member Author

Done. We might wanna add some CI tests that use all the strict tsconfig settings. I'll try and get around to that at some point.

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

No branches or pull requests

4 participants