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

TypeScript issue when import #10

Closed
shian15810 opened this issue Nov 15, 2017 · 3 comments
Closed

TypeScript issue when import #10

shian15810 opened this issue Nov 15, 2017 · 3 comments
Assignees
Labels

Comments

@shian15810
Copy link

When I import wretch according to the test file and polyfill it as the following,

import * as formData from 'form-data';
import { URLSearchParams } from 'url';
import nodeFetch from 'node-fetch';
import wretch from 'wretch';

wretch().polyfills({
  fetch: nodeFetch,
  FormData: formData,
  URLSearchParams,
});

it complain that wretch.default is not a function when build with tsc.

wretch.default().polyfills({
TypeError: wretch.default is not a function

And when i change it to the syntax below,

import * as wretch from 'wretch';

it works, but the linter is complaining...

[ts] Cannot invoke an expression whose type lacks a call signature.

So what I do now is

import * as wretch from 'wretch';
import { Wretcher } from 'wretch/dist/wretcher';

((wretch as any)() as Wretcher).polyfills({
  fetch: nodeFetch,
  FormData: formData,
  URLSearchParams,
});

so that the type definition and tsc build keep working again.

Is there anything wrong with my syntax? Or is there any compiler option that is causing this?

@elbywan
Copy link
Owner

elbywan commented Nov 16, 2017

Hi @shian15810,

I think that it is related to this issue : microsoft/TypeScript#2719

As a workaround, I added a default field in the exported function which should make it compatible with the typescript commonjs transpilation and solve your problem hopefully !

I committed on the dev branch, could you check it out by running npm install https://github.com/elbywan/wretch#dev and tell me if everything is okay ?

Thanks !


Also, here are the files I used to reproduce (it may help) :

tsconfig.json

{
    "compilerOptions": {
        "target": "es5",
        "lib": [ "es2015", "dom" ],
        "module": "commonjs",
        "outDir": ".",
        "moduleResolution": "node",
        "declaration": false,
        "noImplicitAny": false,
        "sourceMap": false,
        "types": [ "node" ]
    },
    "files": ["test-node.ts"]
}

test-node.ts

import * as formData from 'form-data';
import { URLSearchParams } from 'url';
import nodeFetch from 'node-fetch';
import wretch from 'wretch';

wretch().polyfills({
    fetch: nodeFetch,
    FormData: formData,
    URLSearchParams,
});

wretch("http://google.com").get().text(_ => console.log(_))

test-node.js (compiled with tsc)

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var formData = require("form-data");
var url_1 = require("url");
var node_fetch_1 = require("node-fetch");
var wretch_1 = require("wretch");
wretch_1.default().polyfills({
    fetch: node_fetch_1.default,
    FormData: formData,
    URLSearchParams: url_1.URLSearchParams,
});
wretch_1.default("http://google.com").get().text(function (_) { return console.log(_); });

To setup and launch :

npm i https://github.com/elbywan/wretch#dev node-fetch form-data @types/node
tsc -p .
node test-node.js

Output seems fine and no complaints from tsc from my side !

@shian15810
Copy link
Author

Oh yes! No more complaining from tsc in my project anymore.
Anyway, when will dev be merged into master and released to npm?

Also, thanks for your hard work on wretch!
This package has been saving me tons of time dealing with fetch.

@elbywan
Copy link
Owner

elbywan commented Nov 17, 2017

Also, thanks for your hard work on wretch!
This package has been saving me tons of time dealing with fetch.

Many thanks !! 😄

Anyway, when will dev be merged into master and released to npm?

ASAP, meaning today or tomorrow at most !

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

No branches or pull requests

2 participants