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

Improve Typescript typings to allow explicit typing #206

Merged
merged 2 commits into from
Apr 23, 2019
Merged

Improve Typescript typings to allow explicit typing #206

merged 2 commits into from
Apr 23, 2019

Conversation

rafsawicki
Copy link
Contributor

@rafsawicki rafsawicki commented Jun 22, 2018

With current type definitions it's not possible to use explicit typing. I changed them based on this template to allow to import all defined types.

Besides that, I added an AuthenticateError type and changed typescript tests to use explicit typing in a few places. I also changed some names in tests file to avoid naming collisions and to be more in line with other tests.

Not being able to use explicit typing is a problem in my project, so I hope you will find these changes useful.

@rafsawicki
Copy link
Contributor Author

I don't think that my changes are cause of Travis build failing. It failed only on node 10 on some timeout related tests and I haven't touched the core logic at all.

@@ -1,24 +1,25 @@
// relative path uses package.json {"types":"types/index.d.ts", ...}

import Aedes = require ('../..')
import aedes = require ('../..')
import { Client, AuthenticateError } from '../..'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should really expose the server as aedes.Server.

Basically we should add to aedes.js:

aedes.Server = aedes

In this way you can do:

import { Server, Client, AuthenticateError } from 'aedes'

This should not be a semver-major change, but just a facility for those using ESM and Typescript.

Copy link
Contributor Author

@rafsawicki rafsawicki Jun 25, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mcollina what do you think about using default property instead?

Aedes.default = Aedes

This way the main function could be imported using default import syntax, while still exposing types if needed

import aedes, { Client, AuthenticateError } from 'aedes'  
// or Server, whatever default export name someone prefer

In fact, aedes can already be used this way with Babel or even with Typescript with esModuleInterop setting enabled. Unfortunately, by default TS module loader expects default property to be present.

Copy link
Contributor Author

@rafsawicki rafsawicki Jun 26, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, from what I've checked there is no way to change typings in a way that would allow both - current require syntax and default import in Typescript. This means that there would be a breaking change for people that currently use aedes in TS without esModuleInterop enabled.

I still think that default import is the way to go, especially for discoverability - when looking at JS examples, I think it's more obvious that const aedes = require('aedes') translates to import aedes from 'aedes' than to import { Server } from 'aedes', especially since with Babel a default import already works just fine.

If you don't want to introduce breaking changes or don't like default import syntax, I'll change it the way you suggested.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer the way I suggested, as it’s an additive change and it does not require a change in ts configuration.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might have not explained it clearly - it wouldn't require ts configuration change, but change in the way it's currently imported. In any case, I added the Server property as you suggested.

Copy link
Collaborator

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is ok, can you please add the new Server property to the README? And maybe add an example for typescript in there as well?

@sumitkm
Copy link

sumitkm commented Apr 21, 2019

Is this PR waiting on something? Just readme update? I just switched from Mosca to Aedes and I find the TS definition currently released leaves a lot to be desired... this PR would do it a lot of good!

I am happy to help with TS side of things.

@mcollina mcollina merged commit 36ec8b6 into moscajs:master Apr 23, 2019
@mcollina
Copy link
Collaborator

I'll do the quick readme update. Feel free to send more TS prs!

@sumitkm
Copy link

sumitkm commented Apr 23, 2019

@mcollina thanks muchly for merging the PR... I'll update my fork (which seems to be failing a test) and then have a quick run around the TS bindings again.

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.

3 participants