Skip to content

Conversation

@petitcl
Copy link

@petitcl petitcl commented May 17, 2019

Hi !

The goal of this pull request is to fix the typescript module export declaration in types/index.d.ts.
The typescript export that is currently declared does not exactly correspond to the javascript export that is actually made. It still works on some typescript configurations with custom options, but it fails on my system with a very basic configuration.

This is easily fixed by replacing export default split with export = split. This then allow to correctly import this module from typescript like this: import * as split from 'split-string'.

See this link for more informations: microsoft/TypeScript#5565

Thank you !

@doowb
Copy link
Collaborator

doowb commented May 18, 2019

@petitcl according to the typescript language documentation for modules, the current code looks correct and then you would import it using:

import split from 'split-string';

However, those docs are referring to implementation and not definitions, but they do seem to match up with the JavaScript code in this library (e.g. the JavaScript code is exporting a function and the type definitions are saying the default export is a function).

I don't use typescript so I'm not sure what the correct way is, but your change causes the build to fail because there are other type interfaces being exported.

@petitcl petitcl force-pushed the fix/ts-declaration branch 3 times, most recently from 84d10c3 to c700324 Compare May 20, 2019 09:26
@petitcl petitcl force-pushed the fix/ts-declaration branch from c700324 to b035aa0 Compare May 20, 2019 09:38
@petitcl
Copy link
Author

petitcl commented May 20, 2019

@doowb I have made some research, and did some experiments and here what I found:

  • the javascript file exports the split function with this syntax: module.exports = split. This corresponds in typescript to the following import syntaxes: const split = require('split-string') or import split from 'split-string'. It also corresponds to the following typescript export syntax: export = split
  • the typescript declaration file declares something different. According to this declaration, the function is exported with the following typescript syntax: export default split. The corresponding typescript import syntax is import split from 'split-string. The typescript unit tests are written according to this.
  • the typescript unit tests correctly check the typescript syntax, but they do not actually run the javascript code. If they did, they would fail at runtime because of the mismatch between the javascript code and the typescript declaration, that results in the split variable being undefined. This comment explains that behaviour better than me.

So what I did was to update the typescript declaration as well as updating the typescript unit tests. The build is now passing in travis.

Let me know what you think of it.

@doowb
Copy link
Collaborator

doowb commented May 20, 2019

Thanks for the update, I'll take a look at this tonight.

@BenjD90
Copy link

BenjD90 commented Jun 7, 2019

@doowb Have you been able to look at this PR since your last comment ?

@doowb
Copy link
Collaborator

doowb commented Jun 7, 2019

I haven't but I'll try this weekend.

@BenjD90
Copy link

BenjD90 commented Jan 17, 2022

Hello,

Any news about this PR ?

Thanks

@petitcl
Copy link
Author

petitcl commented Jan 25, 2022

Closing this PR as it hasn't been looked at in 3 years and it's adding noise to my global PR list.
If anyone wants to continue this subject, feel free to do so!

@petitcl petitcl closed this Jan 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants