4

Just starting out with Typescript. Made a simple project with a single index.ts file which i want to utilize node-fetch. So my code looks like

index.ts

var fetch = require('node-fetch');
fetch("https://www.google.com")

When running tsc index.ts i get the following

../../../../.nvm/versions/node/v10.14.0/lib/node_modules/typescript/lib/lib.dom.d.ts:17778:18 - error TS2300: Duplicate identifier 'fetch'.

My understanding

Looks like the typescript module installed globally includes types for fetch. I have two questions with regards to this

Questions

  • a) How can I make it so that tsc only looks at my current directory downards for types?
  • b) If i do nothing, what directories is tsc looking in in order to determines types?
  • c) Any obvious flaws to my approach a) worth mentioning?

EDIT: I've also added https://www.npmjs.com/package/@types/node-fetch in hope it will take precedence but no joy

Return-1
  • 2,329
  • 3
  • 21
  • 56
  • 1
    I think the section [@types, typeRoots and types](https://www.typescriptlang.org/docs/handbook/tsconfig-json.html) might have the answer for your questions **but** _fetch_ is defined in the lib.dom.d.ts and [here](https://www.typescriptlang.org/docs/handbook/compiler-options.html) says that if _lib_ is not defined the in the tsconfig,some default libs(DOM included) will be injected. I think in this case just defining "lib": [...] with the libs you are using not including the "dom" would solve the issue... –  Feb 18 '19 at 20:36
  • What is the content of your `tsconfig.json`? – Paleo Feb 19 '19 at 09:07
  • @Paleo its just the basic tsc --init config, not much really. - as we speak im looking into joseph-climber's response, which seems to make sense - – Return-1 Feb 19 '19 at 09:08
  • @joseph-climber unfortunately setting "lib": ["ESNext.Symbol"] completely ignoring mostly everything still yields the same issue. – Return-1 Feb 19 '19 at 09:14
  • Do you want to execute your code on Node.js or in a browser? – Paleo Feb 19 '19 at 10:09
  • node, hence node-fetch – Return-1 Feb 19 '19 at 10:18

2 Answers2

1

First, install typings for Node.js and node-fetch:

npm install -D @types/node@10 @types/node-fetch

Then, use node-fetch:

import fetch from 'node-fetch';
fetch("https://www.google.com");

Notice the presence of the keyword import. An import or an export indicates to the compiler that your file is a ES6 module and not a script. In a module, you can define variables as you want without override global variables.

Paleo
  • 21,831
  • 4
  • 65
  • 76
0

Either list the file or include externally : https://basarat.gitbooks.io/typescript/docs/project/files.html

Amit Golhar
  • 799
  • 4
  • 8
  • 21
  • I attempted this but as suspected it unfortunately doesnt work. It seems like typescript is by definition looking at ```../../../../.nvm/versions/node/v10.14.0/lib/node_modules/typescript/lib/lib.dom.d.ts```. I did try to "exclude": ["../../../../.nvm/**/*"] but it seems odd that i'd have to do this.. joseph-climber's answer made some sense but im still feeling like im missing something. My project is completely new, there's no other modules in it than ```node-fetch``` either – Return-1 Feb 19 '19 at 10:00
  • have you installed typescript globally? if not try to install globally and try – Amit Golhar Feb 19 '19 at 10:43