Closed
Description
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)
Metadata
Metadata
Assignees
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
ghost commentedon Oct 18, 2017
You probably have two declarations of this variable somewhere, could you look for the other one? Find-all-references should help.
mikemaccana commentedon 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 commentedon 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 addingdeclare var process: any;
to a file. You are getting an error at one variable declaration forprocess
, but there is probably another one somewhere in your project.We should improve this error message. #19339
mikemaccana commentedon Oct 20, 2017
I have:
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'sprocess
module as the sensible, well known nameprocess
?ghost commentedon 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 noimport
orexport
statements.However, you can use a module-local variable, using
import process = require("process");
. This has the advantage of being typed asrequire
is just a functionstring => any
if it doesn't appear in animport
declaration.mikemaccana commentedon Oct 20, 2017
Here's a simple two line TS file:
Compiling it:
There is no global 'process' - if I remove the
require
, the resulting JS will printundefined
.Maybe
tsc
is runningnode
using something like node's REPL, which auto-adds certain modules, includingprocess
?PS: not using TC39 modules, just commonJS since that's what 99% of npm uses.
ghost commentedon Oct 20, 2017
process.ENV
is undefined, butprocess
is defined as a global variable in a node environment.tsc
doesn't runnode
, 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, sinceconst x = require("x")
just returnsany
.mikemaccana commentedon Oct 20, 2017
@Andy-MS Ah my bad, it's
env
notENV
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
withimport
?ghost commentedon Oct 20, 2017
Yes, change
const x = require("x");
toimport 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 commentedon Oct 20, 2017
OK thanks. Sorry for wasting your time to what amounted to '
process
is inbuilt now'! 😊mikemaccana commentedon Oct 20, 2017
Is there an equivalent of the node 'require everything at once'?
? 🤔
ghost commentedon Oct 20, 2017
Just use multiple
import
statements.