TypeScript Version: 2.2.2
Code
This is my TSCConfig file content
{
"compilerOptions": {
"module": "amd",
"moduleResolution": "node",
"target": "es5",
"noImplicitAny": false,
"jsx": "react",
"sourceMap": false,
"baseUrl": ".",
"paths": {
"OfficeFabric/*": [
"node_modules/office-ui-fabric-react/lib-amd/*"
]
}
}
}
Expected behavior:
After running npm install @ types/node --save
The project should compile ok.
Actual behavior:
The error message TS2403 is indicated.
Uninstalling the node types fixes the problem.
Could this be a clash between : module:"amd" and moduleresolution: "node"
Current Node version is 7.0.3
The installed types was for version + @types/[email protected]
We would need to know the variable that is causing you this problem, and the places where it is defined.
The error is calling out the index.d.ts Type definition file here.
This file is found here: node_modules\@types\node\index.d.ts
There must be another definition of require somewhere.
I'm using React, when the TSX files are compiled; they produce this JS equivalent as first line of JS file.
define(["require", "exports", "react", "OfficeFabric/components/DetailsList/DetailsList"], function (require, exports, React, DetailsList_1)
Could that be it?
Try using find-all-references on declare var require. It should show you another declare var require with a different type.
Project is not in Visual Studio, rather VS Code, find all require shows no other vars being declared but I'll look again.
declare var noderequire: Require; from vss.d.ts (The VSTS extension SDK)
declare var require : noderequire from index.d.ts (for Node)
TS2403: Subsequent variable declarations must have the same type. Variable 'require' must be of type 'Require', but here has type 'NodeRequire'.
Maybe it's these two that are conflicting?
Not if they have different names (noderequire vs require). Find-all-references should work in vscode -- right click on require.
Ok maybe I found it..... I renamed the var require to nrequire and then searched project for all nrequires. This is what it found:
Looks like there is a variable conflict between Node and Require.
It doesn't make sense to use both of those types at once, since require.js doesn't run on node. Use the --types compiler option to choose just the types you need.
andy,
Thanks so much for your help I think I understand this now!
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.
I had the same error and solved it by manually adding tslint and tslint-react to the package.json. It was installed globally before and so switching node versions with nvm was causing issues.
So what is the proper way to solve this? I see the mention of --types, but...
I started getting this error after upgrading to the latest version of jest, whose typings have the NodeRequire declaration, and my app also has a global declaration of require as follows:
globals.d.ts
declare var require: {
<T>(path: string): T | any;
(paths: string[], callback: (...modules: any[]) => void): void | any;
ensure: (paths: string[], callback: (require: <T>(path: string) => T) => void, chunkName?: string) => void | any;
context: any;
};
tsconfig.json
{
"buildOnSave": false,
"compileOnSave": false,
"compilerOptions": {
"module": "commonjs",
"target": "es5",
"lib": [
"dom",
"es2015"
],
"allowJs": true,
"sourceMap": true,
"jsx": "preserve",
"downlevelIteration": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"strict": true,
"strictNullChecks": false,
"removeComments": true
},
"exclude": [
"node_modules",
"build",
"dist",
"__mocks__",
"coverage",
"*.config.js",
"*.babel.js",
"specs"
],
"types": [
"wwwroot/app/globals.d.ts"
]
}
As I understand it, if types is defined as an array, it should not automatically use globals from node_modules when typechecking (from the Handbook: "If types is specified, only packages listed will be included."), so why do I get this error when I run tsc (and build via webpack)?
For now, I did this, which seems to work, but feels like a hack:
interface AppRequire {
<T>(path: string): T | any;
(paths: string[], callback: (...modules: any[]) => void): void | any;
ensure: (paths: string[], callback: (require: <T>(path: string) => T) => void, chunkName?: string) => void | any;
context: any;
}
interface NodeRequire extends AppRequire {}
@jstaro It looks like Jest defines an interface NodeRequire but doesn't actually declare the require variable -- I think you could just ignore it then?
Hi All,
I'm getting similar error in should npm. Please help me to fix it.
node_modules/should/should.d.ts(237,5): error TS2403: Subsequent variable declarations must have the same type. Variable 'should' must be of type 'Assertion', but here has type 'Assertion'.
node version: v8.9.1
npm version: 5.5.1
"should": "^13.2.1"
@Thavaprakash You probably have 2 such errors -- one in this location and one in another location. For example, you might accidentally have 2 versions of "should" installed that are incompatible.
@andy-ms Thanks for your prompt reply, I don't have 2 versions of "should". I grepped, i found only one place.
# grep 'should: should.Assertion' * -inr
node_modules/should/should.d.ts:237: should: should.Assertion;
@Thavaprakash OK, could you find-all-references on should to look for the other declaration?
should references in many places, we can't dig into that. I tried this, it works but why this global variable
You could also go-to-definition on any of the references and you will probably see two definitions.
Thanks @andy-ms , Issue is fixed
Most helpful comment
andy,
Thanks so much for your help I think I understand this now!