Skip to content

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

Closed
@mikemaccana

Description

@mikemaccana

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)

Activity

ghost

ghost commented on Oct 18, 2017

@ghost

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

mikemaccana

mikemaccana commented on Oct 19, 2017

@mikemaccana
Author

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

ghost commented on Oct 19, 2017

@ghost

@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

mikemaccana commented on Oct 20, 2017

@mikemaccana
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

ghost commented on Oct 20, 2017

@ghost

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

mikemaccana commented on Oct 20, 2017

@mikemaccana
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

ghost commented on Oct 20, 2017

@ghost

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

mikemaccana commented on Oct 20, 2017

@mikemaccana
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

ghost commented on Oct 20, 2017

@ghost

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

mikemaccana commented on Oct 20, 2017

@mikemaccana
Author

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

mikemaccana

mikemaccana commented on Oct 20, 2017

@mikemaccana
Author

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

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

? 🤔

ghost

ghost commented on Oct 20, 2017

@ghost

Just use multiple import statements.

locked and limited conversation to collaborators on Jun 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs More InfoThe issue still hasn't been fully clarified

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @mikemaccana@mhegazy

        Issue actions

          Type error in inbuilt @types/node/index.d.ts · Issue #19293 · microsoft/TypeScript