Skip to content

Type error in inbuilt @types/node/index.d.ts #19293

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

Closed
mikemaccana opened this issue Oct 18, 2017 · 12 comments
Closed

Type error in inbuilt @types/node/index.d.ts #19293

mikemaccana opened this issue Oct 18, 2017 · 12 comments
Labels
Needs More Info The issue still hasn't been fully clarified

Comments

@mikemaccana
Copy link

TypeScript Version: 2.6.0-dev.20171018

Code

 tsc --lib ES2017 .\packages\myapp-database\index.ts

Expected behavior:

Inbuilt types work.

Actual behavior:

 node_modules/@types/node/index.d.ts(48,13): error TS2403: Subsequent variable declarations must have the same type.  Variable 'process' must be of type 'any', but here has type 'Process'.

A user here is having the same issue, however TS folk asked this to be filed seperately: #9725 (comment)

@ghost
Copy link

ghost commented Oct 18, 2017

You probably have two declarations of this variable somewhere, could you look for the other one? Find-all-references should help.

@mhegazy mhegazy added the Needs More Info The issue still hasn't been fully clarified label Oct 18, 2017
@mikemaccana
Copy link
Author

mikemaccana commented Oct 19, 2017

Hi Andy, thanks for the quick reply! The code in node_modules/@types/node/index.d.ts with the error is shipped with TS itself, and seems to be reporting an error in the node stdlib.

I started using Typescript yesterday - how to I match the @types error to relevant code in the node stdlib?

@ghost
Copy link

ghost commented Oct 19, 2017

@types/node isn't shipped with TypeScript itself, it's a separate NPM package. I can't reproduce your error just by installing that. But I can reproduce the error by adding declare var process: any; to a file. You are getting an error at one variable declaration for process, but there is probably another one somewhere in your project.
We should improve this error message. #19339

@mikemaccana
Copy link
Author

I have:

... 
process = require('process'),
...

In my .ts code, and removing it stops the error from appearing. But that's just requiring the module. Why is typescrpt not letting me import node's process module as the sensible, well known name process?

@ghost
Copy link

ghost commented Oct 20, 2017

If there's already a process global variable, you shouldn't assign to it again. A file is assumed to be global if it has no import or export statements.
However, you can use a module-local variable, using import process = require("process");. This has the advantage of being typed as require is just a function string => any if it doesn't appear in an import declaration.

@mikemaccana
Copy link
Author

Here's a simple two line TS file:

const process = require('process');
console.log(process.ENV)

Compiling it:

$ tsc --lib ES2017 .\deleteme.ts
node_modules/@types/node/index.d.ts(48,13): error TS2451: Cannot redeclare block-scoped variable 'process'.
deleteme.ts(1,7): error TS2451: Cannot redeclare block-scoped variable 'process'.

There is no global 'process' - if I remove the require, the resulting JS will print undefined.

Maybe tsc is running node using something like node's REPL, which auto-adds certain modules, including process?

PS: not using TC39 modules, just commonJS since that's what 99% of npm uses.

@ghost
Copy link

ghost commented Oct 20, 2017

process.ENV is undefined, but process is defined as a global variable in a node environment.
tsc doesn't run node, it only compiles code.
process is available as a global variable in all node scripts, not just in the REPL.

import = is not ES6 module syntax, it's TypeScript syntax for importing a commonjs module. Without it, your code will not be typed, since const x = require("x") just returns any.

@mikemaccana
Copy link
Author

@Andy-MS Ah my bad, it's env not ENV and I can see it's there all the time. I swear it had to be imported some time in the past, but oh well. I'll close this.

Are you saying I should replace CommonJS imports using require with import?

@ghost
Copy link

ghost commented Oct 20, 2017

Are you saying I should replace CommonJS imports using require with import?

Yes, change const x = require("x"); to import x = require("x");.
If using multiple exports of a commonjs module you can even do e.g. import { readFile, writeFile } from "fs";.
I would also recommend writing your own modules in an ES6 style even if you're compiling to commonjs.

@mikemaccana
Copy link
Author

OK thanks. Sorry for wasting your time to what amounted to 'process is inbuilt now'! 😊

@mikemaccana
Copy link
Author

mikemaccana commented Oct 20, 2017

Is there an equivalent of the node 'require everything at once'?

import x = require("x"),
   y = require("y")

? 🤔

@ghost
Copy link

ghost commented Oct 20, 2017

Just use multiple import statements.

@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Needs More Info The issue still hasn't been fully clarified
Projects
None yet
Development

No branches or pull requests

2 participants