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

Fix typescript issues with commonjs #124

Closed
wants to merge 2 commits into from

Conversation

ronzeidman
Copy link

Thanks for creating a typescript definition!
I have issues using it though...
I expect to use it like this:

import * as Fuse from 'fuse.js'; // - won't work since the actual "class" is inside fuse - Fuse.Fuse
// or import {Fuse} from 'fuse.js'; // - won't work since there is no actual variable named Fuse inside Fuse
// or import 'fuse.js'; - I can't see the global deceleration if I import it that way since UMD doesn't declare global when commonjs is available
const seachResults = new Fuse(rows ...

all of the above don't work, as a workaround I can do this:

import * as F from 'fuse.js';
interface Type<T> extends Function {
    new (...args: any[]): T;
}
const Fuse: Type<F.Fuse> = <any>F;

but this is not ideal
That's why I've changed the typescript definitions to something I can use out-of-the-box with commonjs.

Thanks for creating a typescript definition!
I have issues using it though...
I expect to use it like this:
```ts
import * as Fuse from 'fuse.js'; // - won't work since the actual "class" is inside fuse - Fuse.Fuse
// or import {Fuse} from 'fuse.js'; // - won't work since there is no actual variable named Fuse inside Fuse
// or import 'fuse.js'; - I can't see the global decleration if I import it that way since the UMD import doesn't declare global when commonjs is available
const seachResults = new Fuse(rows ...
```
all of the above don't work, as a workaround I can do this:
```ts
import * as F from 'fuse.js';
interface Type<T> extends Function {
    new (...args: any[]): T;
}
const Fuse: Type<F.Fuse> = <any>F;
```
but this is not ideal
That's why I've changed the typescript definitions to something I can use out-of-the-box with commonjs.
@avbentem
Copy link

Disclaimer: I am not an expert. Not at all.

I've tested the pull request in webpack (Angular 2, Angular CLI which expects typescript@2.0.10, but same results with 2.1.4), and am getting some errors.

declare const Fuse = fuse.js.Fuse;

...tells me:

Error:(31, 5) TS1038:A 'declare' modifier cannot be used in an already ambient context.
Error:(31, 24) TS1039:Initializers are not allowed in ambient contexts.

And

declare const Fuse: fuse.js.Fuse;

...gives me:

Error:(35, 21) TS2503:Cannot find namespace 'fuse'.

Next, for:

import {Fuse, FuseOptions} from 'fuse.js';

...I get:

TS2305:Module ''fuse.js'' has no exported member 'Fuse'.
TS2305:Module ''fuse.js'' has no exported member 'FuseOptions'.

Using a suggested index.d.ts from Stack Overflow's Why does Webpack fail to find a module when the TypeScript compiler doesn't? with, e.g.,

import {Fuse, FuseOptions} from 'fuse.js';

...or

import * as fuselib from 'fuse.js';

...gets me:

Error:(2, 33) TS2497:Module '".../node_modules/fuse.js/index"' resolves to a non-module entity and cannot be imported using this construct.

Still investigating...

@ronzeidman
Copy link
Author

I'm using import * as Fuse from 'fuse.js';
it works for me without issues under typescript 2.0.10, webpack 2.1.0-beta.28, ts-loader 1.3.2
so your import * as fuselib from 'fuse.js'; should have worked.

@ronzeidman
Copy link
Author

tested the patch using npm i -D ronzeidman/Fuse#patch-1

@aluanhaddad
Copy link
Contributor

aluanhaddad commented Jan 2, 2017

@ronzeidman I am sorry for the confusion. I had a mistake in my answer on Stack Overflow. I neglected to nest the globally defined interface FuseOptions within either the Fuse namespace or within a declare global clause`. I have since updated my answer with a working tested version. I also added comprehensive examples of how to import the module correctly.

http://stackoverflow.com/a/41195089/1915893

@mikeacameron
Copy link

Anyone manage to get this working with Webpack? I've tried everything listed here and SO. Webpack still complains that it cannot find module fuse.js.

@ronzeidman
Copy link
Author

Try using my fork:
npm i -D ronzeidman/Fuse#patch-1
And importing this way:

Import * as Fuse from 'fuse.js'

Should work (that's what I'm doing, and I'm using webpack)

@mikeacameron
Copy link

I get the same error.

Mind sharing your Webpack config?

@aluanhaddad
Copy link
Contributor

My declaration, from SO works. Here it is working, with Webpack 2.
run
npm start
to replace the declaration, run Webpack, serve the app.
https://github.com/aluanhaddad/fusingly

@aluanhaddad
Copy link
Contributor

#129

@mikeacameron
Copy link

mikeacameron commented Jan 12, 2017

@aluanhaddad Thank you!

I got it working using your fix when I converted to Webpack 2.

@aluanhaddad
Copy link
Contributor

Should work fine in Webpack 1 also. Just make sure you set the extension resolver.

@krisk
Copy link
Owner

krisk commented Feb 24, 2017

Is this still needed?

@aluanhaddad
Copy link
Contributor

aluanhaddad commented Feb 27, 2017

Personally, I would say no because it will pollute the global namespace of ambient external module declarations preventing 2 versions of fuse.js from being used side by side without causing type errors. From what I can see, the library supports multi-versioning.

@krisk krisk closed this Mar 6, 2017
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.

None yet

5 participants