lquixada / cross-fetch

Universal WHATWG Fetch API for Node, Browsers and React Native.
MIT License
1.67k stars 104 forks source link

Building with TypeScript fails on types #45

Closed moltar closed 5 years ago

moltar commented 5 years ago
$> tsc --module commonjs

node_modules/cross-fetch/index.d.ts:1:27 - error TS2304: Cannot find name 'fetch'.

1 declare const fet: typeof fetch;
                            ~~~~~

node_modules/cross-fetch/index.d.ts:2:27 - error TS2304: Cannot find name 'Request'.

2 declare const req: typeof Request;
                            ~~~~~~~

node_modules/cross-fetch/index.d.ts:3:27 - error TS2304: Cannot find name 'Response'.

3 declare const res: typeof Response;
                            ~~~~~~~~

node_modules/cross-fetch/index.d.ts:4:31 - error TS2552: Cannot find name 'Headers'. Did you mean 'headers'?

4 declare const headers: typeof Headers;
                                ~~~~~~~

  node_modules/cross-fetch/index.d.ts:4:15
    4 declare const headers: typeof Headers;
                    ~~~~~~~
    'headers' is declared here.

Found 4 errors.
lquixada commented 5 years ago

hi, can you share your tsconfig.json?

moltar commented 5 years ago
{
  "compilerOptions": {
    "moduleResolution": "node",
    "target": "es5",
    "module": "es2015",
    "lib": ["es2015", "es2016", "es2017"],
    "strict": true,
    "sourceMap": true,
    "declaration": true,
    "allowSyntheticDefaultImports": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "declarationDir": "dist/types",
    "outDir": "dist/lib",
    "typeRoots": ["node_modules/@types"],
    "types": ["jest-extended"]
  },
  "include": ["src"]
}
lquixada commented 5 years ago

tks! that's very helpful! gonna take a look!

lquixada commented 5 years ago

So, it seems that when you specify the "es5" target in your compilerOptions, typescript uses the following libs: DOM, ES5, ScriptHost. But since you're overriding these with ["es2015", "es2016", "es2017"] in your lib option, the solution would be adding "dom" to the list:

{
  "compilerOptions": {
    // ...
    "target": "es5",
    "lib": ["es2015", "es2016", "es2017", "dom"],
    // ...
  },
  // ...
}
lquixada commented 5 years ago

Closing this issue since there was no response.

moltar commented 5 years ago

Oh, sorry, not sure how I missed the notification, but I didn't see the reply.

Thank you for your input. The dom declaration definitely fixes the issue for me.

Thanks again for the great module.

lquixada commented 5 years ago

@moltar no worries. I'm glad that it worked out for you! thanks for the reporting the issue.